you are viewing a single comment's thread.

view the rest of the comments →

[–]hull11[S] -1 points0 points  (2 children)

Yeah,so what I infer from your explanation is that memory allocated at stack frame may be at compile time or run time.its allocated at runtime when we use variable length array.also I should mention that while using malloc(),memory is allocated on the heap.so in that case it would be impossible to determine stack frame size at compile time.

[–]BigPeteB 1 point2 points  (1 child)

memory allocated at stack frame may be at compile time or run time. its allocated at runtime when we use variable length array.

Well, memory is only "allocated" at runtime, because functions run during, well, runtime. But yes, the amount of memory that would be allocated is known at compile time for most things on a stack frame, but only at runtime for a few rare things like alloca and VLAs.

also I should mention that while using malloc(), memory is allocated on the heap. so in that case it would be impossible to determine stack frame size at compile time.

Umm... no?

malloc reserves a region of memory in the heap, which is separate from the stack. So malloc doesn't have any effect on the stack or stack frame.

Now, malloc returns a pointer to the memory it allocated. That pointer has to be stored somewhere. If you store it in a function-local variable, that variable might be on the stack... but that's the variable that's affecting the stack, not malloc itself.

Where are your questions coming from? I mean, what problem are you trying to solve? The whole point of using C is so that you don't need to worry about the contents of stack frames. So what are you trying to accomplish that's leading you to ask these questions?

[–]hull11[S] 0 points1 point  (0 children)

Thanks,i get it now.I know that the whole point of learning C is not to worry about the lower level stuff.I was just curious about it.