Gorgan Razvan
Gorgan Razvan

Reputation: 21

Malloc return 0?

I'm doing a homework project in C using the Dev-C++ compiler. Strangely, I've stucked at a malloc function that returns me 0 which would mean that it is out of heap memory. I hardly doubt that the memory is full. I've included the windows.h library.

This is the problematic part of the function. The printf gives me:

00000000 ... 00000000 .... 005C1058

What is wrong here?

typedef struct tcat {
    char *namec;
    struct tcat *nextc;
    struct tprod *firstp; 
}CTGR;

typedef struct tprod {
    char *namep;
    float price;
    int qt;
    struct tprod *nextp;
}PRDCT;

void Srch(int i)
{
    //data base population
    int k,r;

    //categories of components
    CTGR *p,*primc;
    primc = (CTGR *)malloc(sizeof(CTGR));

    primc -> namec = "Resistances";
    p = primc -> nextc;
    p -> namec = "Capacitors";
    p -> nextc = 0;


    PRDCT *q,*primRP,*primCP;
    primRP = (PRDCT *)malloc(sizeof(PRDCT));
    primCP = (PRDCT *)malloc(sizeof(PRDCT));

    printf(" %p ... %p .... %p",primRP,primCP,primc);
    getch();

Upvotes: 0

Views: 727

Answers (1)

Bjorn Munch
Bjorn Munch

Reputation: 496

p = primc -> nextc;

May be the culprit. That value is not set and could be anything, so when you assign through it, you're writing into some unpredictable place in memory.

Upvotes: 3

Related Questions