🔒 Locked
sizeof
Advertisement
Original Post
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
Great - thanks
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.
But as I said, I may be wrong.
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
http://mihail121.tripod.com
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
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
[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
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]
[edited by - HFX on September 5, 2003 5:13:35 PM]
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]
Come on, just try out on your own instead of asking people about it. It IS only ONE call to make.
There's more to program for than just Intel x86.
[edited by - antareus on September 5, 2003 5:39:28 PM]
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]
sizeof(char) is always 1, no matter what. [How To Ask Questions|C++ library of container classes, algorithms and iterators. It provides many of the basic algorithms and data structures of computer science." target="_blank">STL Programmer''s Guide|C++ Style and Technique FAQ - Designer and implementor the C++ programming language." target="_blank">Bjarne FAQ|C++ FAQ Lite - Frequently Asked Questions about the C++ language." target="_blank">C++ FAQ Lite|C++ Reference - The standard C++ library is a collection of functions, constants, classes and objects that extends the C++ language providing basic functionality to interact with the operating system and some standard classes, objects and algorithms that may be commonly needed." target="_blank">C++ Reference|MSDN]
quote:
Original post by dalleboysizeof(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]
"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
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
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.
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.
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.
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.
quote:
Original post by Brother Bobquote:
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
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.
Advertisement