Ever Since I learned pointers

Started by
20 comments, last by Tradone 17 years, 12 months ago
stack is like ram, heap is like virtual memory (hard drive).
sig
Advertisement
Quote:Original post by pcxmac
stack is like ram, heap is like virtual memory (hard drive).
(:-

You should look into those terms a bit, as you seem to be confused about a couple of things.
Quote:Original post by Tradone
and there seems to be a difference between the free store and the heap. I thought there were the same things.


Free store is the general term. "The heap" refers to a specific way the free store can be implemented. People refer to "the heap" when they should talk about free store for mostly historical reasons. And then, there's "the arena" ...

Quote:Original post by pcxmac
stack is like ram, heap is like virtual memory (hard drive).


Not at all, no.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
There is never any reason to declare basic types (ints, booleans, doubles, chars) on the heap. After that, things get a bit language specific. Java requires using the heap, C++ doesn't. But Java has garbage collection. C++ doesn't.

In general, when working with C++, try to declare everything you can on the stack, because it is MUCH harder to make a mistake. If you make a game which allocates a large array when you start a game -- things get UGLY if you forget to free it. You won't notice anything wrong when you run the program the first time, but if you play your game long enough, it is likely it will eventually run out of heap memory and crash. This is why Windows98 is famous for the Blue Screen of Death.
not even in a loose sence?
sig
Quote:Original post by pcxmac
not even in a loose sence?


Nope. Care to elaborate on your comparison. If you are talking purely in "relative amounts of space available", then maybe, but you should specify that.
Is the free store resident inside the process or outside?
sig
Quote:Original post by Tac-Tics
There is never any reason to declare basic types (ints, booleans, doubles, chars) on the heap.

There is a reason: if a basic type has to live over the end of a scope

Quote:Original post by pcxmac
Is the free store resident inside the process or outside?


"Free store" is a generic term that covers both in-process and out-process memory. If you use, for example, shmalloc() to allocate a chunk of shared memory, it is still said to come from the free store, just as if you had used malloc(), LocalAlloc(), GlobalAlloc(), VirtualAlloc(), HeapAlloc(), mmap() ...

In C++, new is said to allocate from the free store (while malloc() is generally said to allocate from "the heap") because new's behaviour can be modified on a global or class-by-class basis via overloading. So there is now way to tell, a priori, where the memory comes from; in fact, you can even use new to create objects in a chunk of memory that actually comes from the stack...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks.

sig

This topic is closed to new replies.

Advertisement