Re: stack and heap



Roman Mashak wrote:
I'm confused about heap and stack memories management in C language. Most books explain that local stack variables for each function are automatically allocated when function starts and deallocated when it exits. In contrast, malloc() always takes memory in the heap. Now, let's consider the code as follows:

int get_buffer()
{
unsigned int head; // local variable?
char *buf; // also local?

char = malloc(100); // allocate memory from heap
...
}

Here is confusion: "char *buf" is supposed to be allocated in stack, on the other hand, malloc() get memory from heap? How to understand this?

That's a good question, and luckily it's not too hard to see why.
The answer is that at this point, right after you have called malloc(),
there are actually TWO pieces of memory that have been allocated.
On a typical 32-bit system, there is a 4-byte region allocated for
the pointer (for "buf"), and those bytes come from the stack. Then
there is a second 100-byte region allocated from the heap. There is
no variable name for this 100-byte region, though. (That is part of
why you have to keep track of the memory yourself and remember to
deallocate it.)

> Is this
> true, that whenever we call malloc (inside of function or outside), we get
> memory from heap?

Yes, malloc() will always return memory from the heap. It doesn't
depend on where it's called from. (Technically, it could use some
other data structure to maintain the pool of memory that it draws from,
but it's still informally called "the heap" even if that's true.)

- Logan
.



Relevant Pages

  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)
  • Re: ten thousand small processes
    ... Stack needs to be executable for the current signal trampoline ... the use of malloc() that is causing your primary ... if there is any heap memory in use at all, no matter what you do, ... either directly, as a 4M page mapping (not used for user processes, ...
    (freebsd-performance)
  • Re: Error Raising and Memory in VB (general question)
    ... > object is terminated go out of scope, and the memory is also released. ... But why are you saying it uses stack? ... I think we are dealing with heap memory here. ... "COM's IMalloc allocator: ...
    (microsoft.public.vb.general.discussion)
  • Re: Linked List & Dynamic Memory Allocation
    ... Both of you mentioned stack and heap in this ... when I call malloc it uses heap to allocate memory. ... if you have an integer pointer object that lives beyond this scope and you had: ...
    (microsoft.public.vc.mfc)