|
Re: I Have A Question About Pointers
Predicting how much memory is used for a given bit of code requires quite a bit of knowledge about the machine architecture. One of the most common tradeoffs in electronics and computer is "space for speed". This is true in your code, but often true in the computer architecture itself.
As an example of this, how much space is taken by
struct { int8 a; int16 b; }[1000]?
The naive answer is (1+2)*1000, but on many architectures, alignment rules force the int16 to be on a two byte alignment and for the struct to actually take four bytes. Additionally, when you malloc you'll find both internal and external fragmentation. You will often find memory managers making all sorts of tricks to avoid locks, to speed up contention for the free list, etc.
Additionally, while a pointer represents space, if they are in a local scope they may actually be just a register and use no memory. This is also true of ints, floats, and any local of sufficiently small scope.
For future reference, if you use sizeof(int*) or any other type, that is a good estimate for memory usage, but all of the factors listed above and many more make an impact.
Greg McKaskle
|