Is this Size Deduction correct

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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
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
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
#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
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
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
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
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
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
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.

• 17
• 9
• 15
• 13
• 60