473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

sizeof struct

tom
hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
t.
Oct 24 '06 #1
7 3769

"tom" <ee@w.ewrote in message news:eh**********@nemesis.news.tpi.pl...
hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
t.

i think 260 is right
Oct 24 '06 #2
tom
I am using VC++ .NET

t.
Oct 24 '06 #3
i see
because __int64 x2; // 8

so sizeof(w1)/8 must integer

my englilsh poor,
do you know what i want to say?
Oct 24 '06 #4
tom wrote:
hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
Google for member alignment. Most probably yours is set to 8 bytes, so
the x2 member will have an offset of 256 in your struct.

Regards,
Stuart
Oct 24 '06 #5

tom wrote:
hiw is calculated size of struct
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
// int DataSize = sizeof(w1);
t.
Padding, 260/8 = 32.5 and 264/8 = 34
for the same reason that a class like:

struct C
{
char c;
int n;
};

int main()
{
size_t size = sizeof(C);
std::cout << "size = " << size << std::endl;

return 0;
}

will also have a size matching the platform's memory indexing
architecture (thankfully).
Probably 8 but not neccessarily.
Regardless, it shouldn't matter in your case, unless you are
byte-copying the contents + padding around. Which you should not be
doing anyways.
Thats why operators and streams were created for.

Oct 24 '06 #6

"tom" <ee@w.ewrote in message news:eh**********@nemesis.news.tpi.pl...
>I am using VC++ .NET
That's nice. Is there some reason you're telling us all what compiler
you're using? Perhaps you're following up on a previous post? If so, then
you should be quoting the post you're responding to.

-Howard
Oct 24 '06 #7
tom wrote:
hiw is calculated size of struct
Size of members, plus size of padding. Padding is any memory used for
internal
compiler purposes. E.g. you'll often see a difference if a struct has
virtual functions,
but also if the members have mixed type.
for example:

struct w1
{
char x1[252]; // 252
__int64 x2; // 8
// should be 260
// is 264; WHY ??
};
Note that you have mixed types here.

My guess in this case is that __int64 is a compiler-defined type that
has
alignment requirements. If it must start at an address that's a
multiple of 8,
then struct w1 must also. In an array of w1's, each w1 should. This in
turn
means the sizeof(w1) must be a multiple of 8.

It may also be a speed optimization - again a 'internal compiler
purpose'.

HTH,
Michiel Salters

Oct 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
12
by: news.fe.internet.bosch.com | last post by:
Hi All , I u find out size of struct , does it considers paddding chars into consideration struct A { char c; int i; };
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
8
by: Chameleon | last post by:
I have a TGA image header struct. TGA has 18 bytes header, so the C struct too. why this return 20? sizeof(TGAHeader) I saw this in many structs. I believe compiler round up the size to 4...
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
14
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.