Skip to main content
gamedev.net
🔒 Locked

sizeof

Started by Austrian Coder Sep 5, 2003 at 3:43 PM 36 replies 3k views
Original Post
Austrian Coder
Austrian Coder
Does sizeof(..) return the size in bytes? In the msdn (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_sizeof_operator.asp) i could not find the answer... Thanks, Christian
cmptrgear
cmptrgear
yes

"I may not agree with what you say but I will defend to the death your right to say it."
--Voltaire
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
Brother Bob
Brother Bob
I may be wrong, but doesn''t sizeof return the size in multiples of sizeof(char)? Since a char is not guaranteed to be a byte long, sizeof is therefore not guaranteed to return the size in bytes. Not that I know of any platform where a char != byte, but anyway.

But as I said, I may be wrong.
cmptrgear
cmptrgear
Straight from msdn

The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.

The address for the documentation

Clicky


Now I made an assumption that he was referring to sizeof in C/C++ if you are using some other language then I dont know.

"I may not agree with what you say but I will defend to the death your right to say it."
--Voltaire
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
Ekas78
Ekas78
[Lightbulb moment] Kaaa-tchiiing! Why don''t you just try it out??? It''s *one* call and you''d know.

quote:
Original post by Austrian Coder
Does sizeof(..) return the size in bytes?

In the msdn (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_sizeof_operator.asp) i could not find the answer...

Thanks, Christian


______Only dead fish go with the main-stream.
HFX
HFX
Brother Bob: You obviously have only used X86 architecture. However, as far as I know, sizeof always returns the size in number of bytes it would take to correctly store the object. So something like Sparc architecture would end up probably using more bytes due to the need for keeping things on full word boundries.


[edited by - HFX on September 5, 2003 5:13:35 PM]
My name is my sig.
Brother Bob
Brother Bob
quote:
Original post by Mihail121
you can ALWAYS assume that char will be 1 byte as you can always assume that word and dword will be 2 and 4 bytes.


When coding on a "normal" computer this may be true, but I've done the mistake of assuming a certain size when coding on smaller platforms like PIC-processors and singal processors. I've seen both 24-bit int and 48-bit ints. Took me a while before I realized my mistake, and since then I generally don't assume anything, I want to know.

quote:
Original post by cmptrgear
Straight from msdn

The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.


I assume that information is specific to MSVC, which have char == byte. But that doesn't mean it's always true.

edit: Eh, may sound a bit stupid when I say above I don't assume anything but want to know, and then I say I assume it was MSVC specific info. What I wanted to say was more along the line, "But isn't that specific to MSVC?".

[edited by - Brother Bob on September 5, 2003 5:32:28 PM]
antareus
antareus
Come on, just try out on your own instead of asking people about it. It IS only ONE call to make.

quote:
Original post by Mihail121
you can ALWAYS assume that char will be 1 byte as you can always assume that word and dword will be 2 and 4 bytes.

http://mihail121.tripod.com



There's more to program for than just Intel x86.

[edited by - antareus on September 5, 2003 5:39:28 PM]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Fruny
Fruny
quote:
Original post by dalleboy
sizeof(char) is always 1, no matter what.



Yes, but the point was that it could be "one banana", not necessarily "one byte"
But if it was so, pointer arithmetic would work in "banana" units, so, from a practical standpoint, it rarely matters.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on September 5, 2003 5:55:04 PM]
"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
MDI
MDI
quote:

It IS only ONE call to make.



sizeof is an operator, not a function.



Tear me apart and boil my bones, I will not rest till she''s lost her throne, my aim is true, my message is clear, it''s curtains for you, Elizabeth, my dear.
sBibi
sBibi
"you can ALWAYS assume that char will be 1 byte as you can always assume that word and dword will be 2 and 4 bytes."

yes... I might be wrong but that''s a C/C++ characteristic

char -> 1 byte
short -> 2 bytes
long -> 4 bytes

doesn''t matter what architecture it''s on, it''ll always be like that

_but_

int -> sizeof(void*) -> same number of bits as the CPU, 32 bits on an x86, 64bits on an alpha
long long -> 2 * sizeof(int) -> 64 bits on an x86, 128 bits on an alpha
JoeyBlow2
JoeyBlow2
Using a struct in MSVC++ will always round up to a multiple of 8 bytes by default (this can be changed in the settings) so if you have:

struct me
{
char data[3];
};

a sizeof(me) will return 8... However, if you do a sizeof(me.data) it will return 3.

Just something to keep in mind.
Miserable
Miserable
Sizes (as returned by the sizeof operator) are expressed in terms of sizeof(char); thus by definition, sizeof(char) is 1. A char is guaranteed to be at least 8 bits. (Stroustrup, TCPL, 4.6) This is all that is guaranteed. Stroustrup also states that a char is typically one 8-bit byte.
Namethatnobodyelsetook
Namethatnobodyelsetook
quote:
Original post by sBibi
"you can ALWAYS assume that char will be 1 byte as you can always assume that word and dword will be 2 and 4 bytes."

yes... I might be wrong but that''s a C/C++ characteristic

char -> 1 byte
short -> 2 bytes
long -> 4 bytes


It has nothing to do with C/C++, and it''s wrong. Granted, it will work on 99% of computers in the world, but it''s still wrong.

On the TMS320C30 DSP the following is true:
sizeof(char)==1
sizeof(short)==1
sizeof(int)==1
sizeof(long)==1
sizeof(float)==1
sizeof(double)==1
sizeof(void *)==1

Each addressable unit of RAM is 32 bits wide, making EVERY type 32 bits. They took the liberty of making double 32 bits, same as a float, which is a pity. It''s all valid according to the C/C++ spec, but it''s very different from what a typical coder expects.

This also means you can''t access specific bytes of a short or long by moving a char pointer around.

It takes some thought, and practice, to get used to programming code that works on this DSP and x86, but it''s possible. At a former employer, our entire product line was written to work correctly on the real hardware, and a DOS (and later Windows) environment.
Mihail121
Mihail121
quote:
Original post by Brother Bob
quote:
Original post by Mihail121
you can ALWAYS assume that char will be 1 byte as you can always assume that word and dword will be 2 and 4 bytes.


When coding on a "normal" computer this may be true, but I''ve done the mistake of assuming a certain size when coding on smaller platforms like PIC-processors and singal processors. I''ve seen both 24-bit int and 48-bit ints. Took me a while before I realized my mistake, and since then I generally don''t assume anything, I want to know.

quote:
Original post by cmptrgear
Straight from msdn

The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.


I assume that information is specific to MSVC, which have char == byte. But that doesn''t mean it''s always true.

edit: Eh, may sound a bit stupid when I say above I don''t assume anything but want to know, and then I say I assume it was MSVC specific info. What I wanted to say was more along the line, "But isn''t that specific to MSVC?".

[edited by - Brother Bob on September 5, 2003 5:32:28 PM]


You may see that i''m pointing only the dword/word/byte.I pretty much know that size of the int types vary on different platforms.For DOS(check with TurboC) it''s 16,for Win32 and etc.But if we cannot be sure that the byte/word/dword will always be 1/2/4 there is no sence to program then

http://mihail121.tripod.com

__fold
__fold
sizeof(char) is always 1?
A unicode char is 2 bytes. So I guess that some compiler''s might say that sizeof(char) is 2.

Mathematix
Mathematix
quote:
Original post by JoeyBlow2
Using a struct in MSVC++ will always round up to a multiple of 8 bytes by default (this can be changed in the settings) so if you have:

struct me
{
char data[3];
};

a sizeof(me) will return 8... However, if you do a sizeof(me.data) it will return 3.

Just something to keep in mind.


Careful how you use the sizeof operator!

int aInt, a;

a = sizeof(int); // Gives you the size in bytes of an integral type.
a = sizeof aInt; // Gives you the size in bytes of the int variable aInt.

I recall posting this before.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.