I'm going through some lecture slides from Princeton University and have a question. The professor has this code snippet (on slide 8 there):
struct Table *Table_create(void) {
struct Table *t;
t = (struct Table*)malloc(sizeof(struct Table));
t->first = NULL;
return t;
}
struct Table *t;
…
t = Table_create();
…
In the Table_crate() function, even though t is allocated using malloc, t itself would be located on the stack, correct?.
So, can you return t from this function? I'd think t in Table_create() would disappear as soon as the function returns.
mallocis allocating on heap. I'ts perfectly fine to return it's result from a function. the only thing, that the prof should not cast the return result frommalloc...Table_crate()function.mallocand friends!. That probably wasn't the reason for a down-vote, seems like someone just DV'ed every answer on this page..