Hi, how do I know how many bytes I've initialised to a datatype?
ie:
int x; <--Could this mean I've reserved 1 byte?
How do I initialize a fix length of bytes?
Hi, how do I know how many bytes I've initialised to a datatype?
ie:
int x; <--Could this mean I've reserved 1 byte?
How do I initialize a fix length of bytes?
Use sizeof() perhaps?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
See Salem's answer.Hi, how do I know how many bytes I've initialised to a datatype?
ie:
int x; <--Could this mean I've reserved 1 byte?
BTW, instead of "how many bytes [have I] initialised to a datatype", you might more properly put it as "how many bytes were reserved [on the stack] when I declared this variable".Code:int x; printf("%d\n", sizeof(x));
I have no idea why you would want to do this, but an ordinary local array of characters should require CHAR_BIT/8*elements bytes. CHAR_BIT is usually 8, soHow do I initialize a fix length of bytes?
should usually allocate 16 bytes. (This won't work in structures if that's what you're thinking.)Code:char dummy[16];
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
In C/C++, a byte is defined as whatever the size of a (signed or unsigned) char is, so your definition always allocates 16 bytes. The catch, of course, is that a byte (in C/C++) isn't always 8 bits, but for many purposes, such as calling malloc(), it doesn't matter.
>sizeof char is 1, not 1 byte, just 1...
sizeof returns the size in bytes.
No it doesn't. It returns the size in chars. [edit] <-- this is wrong [/edit]
[edit]
Hang on...
Give me a second, I'll try to find my point of confusion. I think there's a reference book I have around here some place that says it's "size in chars", which was fresh in my mind, which is why I thought to double check it once more.6.5.3.4 The sizeof operator
Constraints
1 The sizeof operator shall not be applied to an expression that has function type or an
incomplete type, to the parenthesized name of such a type, or to an expression that
designates a bit-field member.
Semantics
2 The sizeof operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of
the operand. The result is an integer. If the type of the operand is a variable length array
type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
integer constant.
[/edit]
[edit2]
I may be confusing another thread. I'm getting too old I guess.
[/edit2]
Quzah.
Last edited by quzah; 03-31-2007 at 11:02 PM.
Hope is the first step on the road to disappointment.
The MSVC 2005 documentation says that (in C) sizeof returns the number of bytes and (in C++) sizeof returns the size with respect to char.
Don't quote me on that... ...seriously
Maybe it's something like that I was thinking of. I'll blame old age and no cup of morning coffee before posting. Here is another FAQ type read, but it relates to C++.
If I had been awake enough, I wouldn't have edited that, taken advantage of the upcoming date, and tried to pass it off as an elaborate ruse.
Quzah.
Last edited by quzah; 03-31-2007 at 11:39 PM.
Hope is the first step on the road to disappointment.