Archived

This topic is now archived and is closed to further replies.

FrigidHelix

How does sizeof() work?

Recommended Posts

Consider the following application of sizeof():
  
int someArray[10];
cout << (int)sizeof(someArray); // outputs 10]

  
How does sizeof() figure out that there are 10 elements in someArray? I thought that when an array is allocated, it is basically just a pointer to the first element of the array, and you arrive at other elements in the array by simply incrementing this pointer, since an array is a contiguous chunk of memory. Does the program keep some sort of reference table of the size of its variables? Or is this something done by the compiler at compile time? If the size of arrays is actually stored then, does that mean that if you were to have 100,000 arrays each containing only 2 elements, as opposed to 1 array of 200,000 elements, the former would use up more memory? Does that mean, that although you instruct the compiler to allocate 200,000 bytes (assuming it is an array of chars), depending on how many arrays you allocate, more memory may actually be used for those variables? I am confused on how this works, can someone please explain this to mean, and not scoff too much if I am completely off target and sounding ridiculous? thx.

Share this post


Link to post
Share on other sites
sizeof() is a compile-time thing, no database is required in your program to use it.

When you declare a static array like that, the compiler knows at build-time how many elements are in the array and how big each element is, therefore when it resolves uses of the sizeof operator it can tell how big the array is.

If you declare a pointer and dynamically allocate it to point to an array then sizeof will not return the correct size, but will just return the size of a pointer (4 bytes on 32 bit machines).

Share this post


Link to post
Share on other sites
If you allocate an array dynamically you can use the function _msize() to find out its size (at least on windows).

Share this post


Link to post
Share on other sites
To help clarify things a bit - sizeof() is not actually a function call, it is more of a macro. The whole call is actually replaced with the constant value that the call resolves to. Just like a #define macro. So, like was just mentioned, it can only function with values and type that are known at compile time.

Hope this helps a bit more,
Landsknecht

Share this post


Link to post
Share on other sites
Be careful using _msize() to find out the size of a dynamic array.

First of all, _msize() returns the number of bytes, so to get the number elements, you would neer, for example:

  
int *array = malloc( 100 );
...
int numElements = _msize( array ) / sizeof( *array );


But most disturbing of all, _msize() returns a different number in Debug builds, compared to Release builds! This is because the Debug build may allocate more memory on either side of the memory you actually get, to test for buffer over- and under- runs.



War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
I have an old C book and it says its a function call that figures out the size of an array using the stdlib.h libary. Maybe its in both and became used differently later? I was also surprised that Borland C++ bolds the word when I type it in being that I dont think sizeof is a keyword in C/C++.

But the way how sizeof COULD figure out the number of elements in an array is first knowing how large is element is, and how big all of the elements is. As soon as you have both peices of information divide the total size of the array by the size of the number of elements. Like this(I dont know how to make a message box on this boards):

char str[255] = "David is a cool programmer";

int num_elements = sizeof(str) / sizeof(char);

That array is 104 bytes. At least it should be the number sizeof(str) returns. Divided by the size of each element(char data type) which is 4-bytes(on 32 bit systems) gives you 26 which is the number of elements in that array assuming I counted correctly.

Now I have to be honest. I had to use the algorithm I just explained to find out that the array was 104 bytes. The compiler knows how many bytes that is and you work from there to find to find out the number of elements - not the reverse.

Also I dont think that you need the typecast of (int) for sizeof; I think it returns an int in number of bytes. I guess sizeof can work with dynamically allocated arrays if you include the stdlib.h library. I would have to refer to my C programming book which someone is borrowing now to make sure.

Note that you can also use the strlen function in the string.h library. It doesn't count the null terminator though.

"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Edited by - EbonySeraph on June 12, 2001 12:57:38 AM

Share this post


Link to post
Share on other sites
In C++, sizeof is an operator that cannot be overloaded. It returns the size (in bytes) of a type or expression. The return type is size_t. The compiler figures out the size at compile time, so there''s no run time thing going on.



Some useful C++ links:
Free multiplatform ANSI C++ Standard Library implementation
Visual C++ STL fixes
Visual C++ 6.0 noncompliance issues
C++ FAQ Lite

Share this post


Link to post
Share on other sites
The compiler comiles your code. The compiler knows how big an int is. You do not necessarily know how big an int is. Ints used to be 16 bits on older lightweight systems. Today, most systems are probably using 32 bits for an int.

The point is, when you code an expression like this:

buffer = malloc (sizeof (int) * 1000);

the compiler is for all intents and purposes replacing your code with this (assuming the target platform uses 4 byte ints):

buffer = malloc (4000);

and then compiling that code into an executable.

The expression is NOT evaluated at runtime.

Share this post


Link to post
Share on other sites
quote:
Original post by EbonySeraph
That array is 104 bytes. At least it should be the number sizeof(str) returns. Divided by the size of each element(char data type) which is 4-bytes(on 32 bit systems) gives you 26 which is the number of elements in that array assuming I counted correctly.



sizeof(char) is 8 bits = 1 byte, not 4.




War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I compiled the following in VC++ 6 and looked at it through the disassembler:


int n = sizeof (int);

asm:
mov dword ptr [ebp-4],4


So it''s evaluating the sizeof at compile time.

Share this post


Link to post
Share on other sites