Is this Size Deduction correct

Recommended Posts

dawidjoubert    161
struct CQuad { CVector3 vert[4]; CVector2 TexCoord[4]; CVector3 Normal; // The Average Normal of the 2 Triangles // int Texture; BYTE x,y; // Its X & Y Tile it belongs 2 bool Passable; }; This things size is 4 * Vector3 + 4 * Vector2 + Vector3 + int + Byte + Byte (12 + 8 + 3)float + 16 + 8 + 8 (12 + 8 + 3)16 + 16 + 8 + 8 = 112 * 8 = 112bytes Is that correct? That is alot isnt it? [Edited by - dawidjoubert on November 28, 2005 7:21:33 PM]

Share on other sites
SiCrane    11839
Then only way to really know what size a struct is, is to use sizeof. The compiler may put in extra padding for alignment. Plus it looks like you left out the bool when calculating size.

Share on other sites
dave    2187
This:
	struct var	{		int a;		float pos[12];		float tex[8];		float n[3];		char bytes[2];		bool passable;	};

Comes to 100 bytes for me. When i took the bool out the size didn't change.

Dave

Share on other sites
JohnBSmall    881
#include <iostream>int main(int argc, char *argv[]){    struct CQuad    {        CVector3 vert[4];        CVector2 TexCoord[4];        CVector3 Normal; // The Average Normal of the 2 Triangles        //        int Texture;        BYTE x,y; // Its X & Y Tile it belongs 2        bool Passable;    };    std::cout << "sizeof(CQuad) = " << sizeof(CQuad) << std::endl;    return 0;}

Compile that, run it, and see what it tells you. The number is in terms of the size of the 'char' type (ie, sizeof(char) = 1). Unless you're on a very unusual system, 'char' will be one byte, so the number this gives you will be the size of the structure in bytes.

Note that the size of the structure may be larger than you expect, because of padding inserted by the compiler to keep things aligned in memory.

Don't assume things about the sizes of structures if you can possibly avoid it - use sizeof() instead of a hard wired constant. And if you absolutely can't avoid it, then check your compiler's documentation to find out how you can force specific padding/alignment settings.

John B

Share on other sites
dawidjoubert    161
oh thanks , no i merely wanted 2 do a small estimate sum..

see if i have 128*128 quads then it only uses 1.86MB memory if the quad is 112Bytes.

Sizeof Returns 100, so i guess i overcalculated.. thats nicely 1.68MB.. so i guess its okay with the bottom user of my program having 256.

Ofcourse eventually all the stuff will add up :-)

And it also means my LOD version which has a maximum division of 1 tile = 16 tiles(quads) (4 x 4) that 128*128*16* float() = 12.6MB which is acceptable :-)

Share on other sites
dave    2187
One thing to note for the future is that you can aid the compiler with alignment to an extent, although it no doubt sorts it all out itself. Put the biggest things at the top and all the nik-nacks after it.

Dave

Share on other sites
iMalc    2466
Quote:
 Original post by dawidjoubertSizeof Returns 100, so i guess i overcalculated.. thats nicely 1.68MB.. so i guess its okay with the bottom user of my program having 256.
It looks like you were assuming that a float is 16 bytes. It's only four, same as an int (on 32-bit). Though it also appears as if you were assuming a byte was 8 bytes, so maybe I'm not reading it right.

Share on other sites
Endar    668
Quote:
 Original post by JohnBSmallUnless you're on a very unusual system, 'char' will be one byte....

What kind of unusual systems have 'char' as not a byte? Some of the older ones?

Share on other sites
ToohrVyk    1595
Quote:
 Original post by DaveOne thing to note for the future is that you can aid the compiler with alignment to an extent, although it no doubt sorts it all out itself. Put the biggest things at the top and all the nik-nacks after it.

The order in which fields are laid out in memory is specified, and implementation-independent. So the compiler does not have the option of rearranging them. Unlike you.

Share on other sites
SiCrane    11839
Quote:
Original post by Endar
Quote:
 Original post by JohnBSmallUnless you're on a very unusual system, 'char' will be one byte....

What kind of unusual systems have 'char' as not a byte? Some of the older ones?

None. char is defined to be one byte.

Share on other sites
Kalasjniekof    246
Quote:
Original post by Endar
Quote:
 Original post by JohnBSmallUnless you're on a very unusual system, 'char' will be one byte....

What kind of unusual systems have 'char' as not a byte? Some of the older ones?

In managed languages as C# a char is 2 bytes. UNICODE forced down your throat.

Share on other sites
JohnBSmall    881
Quote:
 Original post by SiCraneNone. char is defined to be one byte.

Odd... I wonder why they didn't just call it 'byte' then.

John B

Share on other sites
Zahlman    1682
Quote:
Original post by JohnBSmall
Quote:
 Original post by SiCraneNone. char is defined to be one byte.

Odd... I wonder why they didn't just call it 'byte' then.

John B

Backwards compatibility with C.

Back in C days, people did a lot of bad stuff with regards to text handling (in particular, assuming it's ok to represent each character of a string in one byte, and then put a special byte at the end and set a pointer to the beginning) - 'char' and 'byte' became synonymous. In C++, sizeof(char) == 1, i.e. the char size is defined to be one byte, but a byte is *not* guaranteed to be 8 bits (it is, however, guaranteed to be *at least* that much, and also that memory consists of contiguous bytes with no holes in between) - to unambiguously refer to 8 bits in technical discussion, use 'octet'.

Share on other sites
JohnBSmall    881
Quote:
Original post by Zahlman
Quote:
Original post by JohnBSmall
Quote:
 Original post by SiCraneNone. char is defined to be one byte.

Odd... I wonder why they didn't just call it 'byte' then.

John B

Backwards compatibility with C.

No, I meant why was 'char' chosen when it was actually a choice (presumably when C was designed, although maybe even in an earlier language).

John B

Share on other sites
Puzzler183    540
It's not defined as a byte. I don't have a copy of the standard, but it's not.

Share on other sites
CTar    1134
Quote:
 Original post by Puzzler183It's not defined as a byte. I don't have a copy of the standard, but it's not.

Quote:
 The 2003 C++ standard - 5.3.3 sizeofThe sizeof operator yields the number of bytes in the object representation of its operand. The operandis either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall notbe applied to an expression that has function or incomplete type, or to an enumeration type before all itsenumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designatesa bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1;

So sizeof gives the number of bytes. And sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1, which means they are 1 byte.

Share on other sites
Odiee    100
[quote]Original post by Dave
This:
	struct var	{		int a;		float pos[12];		float tex[8];		float n[3];		char bytes[2];		bool passable;	};

and then just
size_t size = sizeof(var);printf("%d",(int)size);

And than you have it, MAN! where do you live?