0

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.

7
  • 4
    malloc is 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 from malloc... Commented Jun 6, 2016 at 15:38
  • You didn't post Table_crate() function. Commented Jun 6, 2016 at 15:40
  • @MikeCAT I found it (click) Commented Jun 6, 2016 at 15:42
  • 1
    @GrzegorzSzpetkowski yes, definitely check for null and free after use. I copied the sample code verbatim from the slides. Commented Jun 6, 2016 at 15:53
  • 1
    @rockford98: When writing C, don't cast the return value of malloc and friends!. That probably wasn't the reason for a down-vote, seems like someone just DV'ed every answer on this page.. Commented Jun 6, 2016 at 16:40

4 Answers 4

3

The variable t has automatic storage duration. But that doesn't prevent you from returning its value from the function. The value itself (i.e. the pointer returned by malloc()) has the lifetime of the program (or until you call free() on it). So, it's not an issue to return the malloc()'ed value from a function.

If it helps, consider this:

int func(int num)
{
    int val;
    val = num * 2; //  take care of signed integer overflow!
    return val;
}

Does returning val here has anything to do with the lifetime of val (which is a local variable and has automatic storage duration)? No. This is analogous to malloc() code you have.

Sign up to request clarification or add additional context in comments.

1 Comment

I think someone is playing games, everybody's answers were voted down :)
1

The variable t lives on the stack, but the thing it points to (via the call to malloc) lives on the heap.

When you return t you are returning it's value, which is the address on the allocated memory. This is then assigned to a local variable in the caller. If you hadn't assigned it in the caller you would have a memory leak. At some point you will need to call free in order to release the memory and avoid the memory leak.

Comments

0

Yes, you can return the value stored in t because the value is copied to the caller, but you shouldn't return pointers or references to t because t will disappear and such pointers or references are useless.

2 Comments

Not sure how you're answer is any different than mine.'
@self I don't see how your answer is similar to Mike's. This answer describes that it is OK to return t because it is copied, and this answer does not make the statement that "t would not be on the stack" which is not exactly correct and may be misleading.
-1

t would not be on the stack. You can return t from the function. malloc will remember the allocated memory until you call free

9 Comments

The local t will be on the stack in typical implementation, or it may be on the register.
malloc doesn't remember, the O/S remembers.
malloc maintains a linked list, at least glibc malloc does
@nicomp It may not be the O/S but the library in user space that remember the memory.
@self I mean struct Table *t; inside the function Table_create() by "local t".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.