Re: stack and heap



On Jun 26, 2:02 pm, "Roman Mashak" <m...@xxxxxxxx> wrote:
Hello,

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? Is this
true, that whenever we call malloc (inside of function or outside), we get
memory from heap?

Perhaps you are really asking:
There are these two cans on the shelf that both say "Memory!" but one
of them says 'automatic' and the other one says 'allocated'. Now,
both of those cans have mouth-watering pictures of delicious memory
tidbits... ready for consumption. Which one to try? Are they both
the same flavor? Do they have any bad aftertastes? Which one will a
real memory *connoisseur* choose?

Both of them are wonderful, both for simple snacks and for the main
course, but they also have some subtleties that you must be aware of.

For either one, you can empty the can, and when you reach in for more
-- too bad! It's empty!! :-(

Now, what happens when it is empty is not the same. The 'allocated'
can just comes up empty, and leaves it up to you about what to do with
the missing ingredient for your recipe. On the other hand, the
'automatic' can has a mean trick. When you try to pour some out of
the empty 'automatic' can, razor sharp shards of shredded program
spill all over the floor. Well then, we'll just use 'allocated' all
the time. But not so fast... The 'allocated' can has a mean streak
of its own. You only get to borrow the memory, and if you forget to
put it back in the can, it can cause all kinds of headaches. Big
lumps of it start loafing around and annoying the other dishes that
are being prepared. Before you know it, it fills the whole room like
some water from the 'Sourceror's Apprentice' and nothing useful can be
done. Not only that, but if you should forget to check if the can is
empty, the "gag" address given to you will cause a heinous falling of
your whole cake, and you'll probably burn the cookies.

Oh, and when cooking with hot stuff, don't forget the oven mits.

.



Relevant Pages

  • stack and heap
    ... I'm confused about heap and stack memories management in C language. ... books explain that local stack variables for each function are automatically ... mallocalways takes memory in the heap. ...
    (comp.programming)
  • stack and heap
    ... I'm confused about heap and stack memories management in C language. ... books explain that local stack variables for each function are automatically ... mallocalways takes memory in the heap. ...
    (comp.lang.c)
  • Re: Lcc-win32 extensions to C
    ... compiled with a C++ compiler or a C compiler. ... Yes, but if you do not use GC, you allocate more memory than needed to ... Using a non-GC'ed heap+ a GC'ed heap requires also more memory... ... Collector, since GC technologies are much more advanced than simple ...
    (comp.std.c)
  • Re: Huge pages and small pages. . .
    ... you need to have them cached in the TLB; if the TLB runs out of ... >>> low end of the heap, until someone figures out a way to tell the system ... >> causing them to be COW'd to real memory. ... I believe the swapper will kill them fast once you have ...
    (Linux-Kernel)
  • Re: Help wanted - problems with heap
    ... Memory pressure, because of limited heap size. ... identified that by failures to allocate memory. ... Any pointers as to work out what is happening would be welcome. ...
    (rec.games.roguelike.development)