Is this Size Deduction correct

Started by
15 comments, last by Odiee 18 years, 4 months ago
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]
----------------------------

http://djoubert.co.uk
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.
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
#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
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
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 :-)
----------------------------

http://djoubert.co.uk
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
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.


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.

This topic is closed to new replies.

Advertisement