Jump to content
  • Advertisement
Sign in to follow this  
dawidjoubert

Is this Size Deduction correct

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by dawidjoubert
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.
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 this post


Link to post
Share on other sites
Quote:
Original post by JohnBSmall
Unless 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 this post


Link to post
Share on other sites
Quote:
Original post by Dave
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by Endar
Quote:
Original post by JohnBSmall
Unless 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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!