By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,785 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,785 IT Pros & Developers. It's quick & easy.

Size of Structure

P: n/a
Hi Group,

I have a structure than has other structures in it. I added 3 members
to a strucuture and my program started behaving very strangly. When I
printed sizes of the strucutures the values were very strange and
unexpected. Here is an example. Applogies for not being able to post
the exact code.

Struct B{
long b1;
short b2;
}

Struct C{
long c1;
char c2;
char *cptr;
}

Struct A{
long a1;
B sb;
long a2;
C sc;
short a3;
char *a4;
}

Now changes struct C to,
Struct C{
long c1;
char c2;
char c3;
char c4;
bool c5;
char *cptr;
}
Then I printed the sizes of my strucutures using sizeof(). These are
actual values from my program.

Before Modification:
Size of Struct A: 814552
Size of Struct C: 8452

After Modification,
Size of Struct A: 815064
Size of Struct C: 8460

Those values obviously looks spurious. I could not access the members
of Struct A correctly. All the members of Struct A declared after
Struct C (in this case a3 and *a4) were off by 256 bytes. I ran this
on two computers just to make sure it is not some strage problem with
machine and got same results.

How can I solve this. Any help is highly appreciated. Thank you very
much.

Reddy
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies

P: n/a

"MSReddy" <ms********@indiatimes.com> a écrit dans le message de
news:67**************************@posting.google.c om...
Hi Group,
Hi,
I have a structure than has other structures in it. I added 3 members
to a strucuture and my program started behaving very strangly. When I
printed sizes of the strucutures the values were very strange and
unexpected. Here is an example. Applogies for not being able to post
the exact code.

Struct B{
long b1;
short b2;
}
Missing ;

Struct C{
long c1;
char c2;
char *cptr;
}
Missing ;
Struct A{
long a1;
B sb;
long a2;
C sc;
short a3;
char *a4;
}
Missing ;
Now changes struct C to,
Struct C{
long c1;
char c2;
char c3;
char c4;
bool c5;
char *cptr;
}


Missing ;

Regis
Nov 14 '05 #2

P: n/a

"MSReddy" <ms********@indiatimes.com> a écrit dans le message de
news:67**************************@posting.google.c om...
Hi Group,
Hi again,

I missed other points,
Struct B{
the keyword is struct, not Struct
long b1;
short b2;
} };

[snipped]
Struct A{ struct A {
long a1;
B sb;
B isn't a type or some aliased one, tou must specify that B stands for a
structure:
struct B sb;
long a2;
C sc;
Same as above:
struct C sc;
short a3;
char *a4;
}

};

[snipped]

Running this:
#include <stdio.h>

struct B{
long b1;
short b2;
};

struct C{
long c1;
char c2;
char *cptr;
};

struct A{
long a1;
struct B sb;
long a2;
struct C sc;
short a3;
char *a4;
};

int main(void)
{
printf("%u\n",(unsigned)sizeof(struct A));
printf("%u\n",(unsigned)sizeof(struct B));
printf("%u\n",(unsigned)sizeof(struct C));
return 0;
}

....I get respectively 36, 8 and 12 with my implementation.

Regis
Nov 14 '05 #3

P: n/a
>Struct C{
long c1;
char c2;
char *cptr;
} ....
Now changes struct C to,
Struct C{
long c1;
char c2;
char c3;
char c4;
bool c5;
char *cptr;
}
Then I printed the sizes of my strucutures using sizeof(). These are
actual values from my program.

Before Modification:
Size of Struct A: 814552
Size of Struct C: 8452


How did you obtain the size of struct C? I find it very difficult
to believe that a struct containing a long, 3 chars, a bool (whatever that is)
and a pointer has a length anywhere near 8452 bytes.

Show the code that printed the above output.

Gordon L. Burditt
Nov 14 '05 #4

P: n/a
"MSReddy" <ms********@indiatimes.com> wrote:
Hi Group,

I have a structure than has other structures in it. I added 3
members to a strucuture and my program started behaving very
strangly. When I printed sizes of the strucutures the values
were very strange and unexpected. Here is an example. Applogies
for not being able to post the exact code.
If you can't post the exact code, at least post code that compiles!
Struct B{
There is no capital on the struct keyword.
long b1;
short b2;
}
Missing semicolon.

[...] B sb;
Undefined type. You must include the struct keyword to reference a struct
tag in C.
struct B sb;

[...] bool c5;


Undefined type. You must include the <stdbool.h> header to use the bool type
on C99. It is not available at all on C89.

When I fixed up your code, I got entirely reasonable values for the struct
sizes on three different compilers.

#include <stdio.h>

typedef int bool;

struct B {
long b1;
short b2;
};

struct C {
long c1;
char c2;
char *cptr;
};

struct A {
long a1;
struct B sb;
long a2;
struct C sc;
short a3;
char *a4;
};

struct C2 {
long c1;
char c2;
char c3;
char c4;
bool c5;
char *cptr;
};

int main(void)
{
printf("sizeof (struct B) == %lu\n", (long unsigned)sizeof (struct B));
printf("sizeof (struct C) == %lu\n", (long unsigned)sizeof (struct C));
printf("sizeof (struct A) == %lu\n", (long unsigned)sizeof (struct A));
printf("sizeof (struct C2) == %lu\n", (long unsigned)sizeof (struct C2));
return 0;
}

--
Simon.
Nov 14 '05 #5

P: n/a
"Ralmin" <ne**@ralminNOSPAM.cc> wrote in message news:<15******************************@news.terane ws.com>...
"MSReddy" <ms********@indiatimes.com> wrote:
Hi Group,

I have a structure than has other structures in it. I added 3
members to a strucuture and my program started behaving very
strangly. When I printed sizes of the strucutures the values
were very strange and unexpected. Here is an example. Applogies
for not being able to post the exact code.


If you can't post the exact code, at least post code that compiles!
Struct B{


There is no capital on the struct keyword.
long b1;
short b2;
}


Missing semicolon.

[...]
B sb;


Undefined type. You must include the struct keyword to reference a struct
tag in C.
struct B sb;

[...]
bool c5;


Undefined type. You must include the <stdbool.h> header to use the bool type
on C99. It is not available at all on C89.

When I fixed up your code, I got entirely reasonable values for the struct
sizes on three different compilers.

#include <stdio.h>

typedef int bool;

struct B {
long b1;
short b2;
};

struct C {
long c1;
char c2;
char *cptr;
};

struct A {
long a1;
struct B sb;
long a2;
struct C sc;
short a3;
char *a4;
};

struct C2 {
long c1;
char c2;
char c3;
char c4;
bool c5;
char *cptr;
};

int main(void)
{
printf("sizeof (struct B) == %lu\n", (long unsigned)sizeof (struct B));
printf("sizeof (struct C) == %lu\n", (long unsigned)sizeof (struct C));
printf("sizeof (struct A) == %lu\n", (long unsigned)sizeof (struct A));
printf("sizeof (struct C2) == %lu\n", (long unsigned)sizeof (struct C2));
return 0;
}


Appologies for all the typos. As I said, the code I gave is just an
example. My structures are big . So the sizes that I showed are
different from sizes of struct A, B and C. Unfortunately, I wouldnt be
able to post my structures, so I made up these structures (A,B and C)
just give an example of what I am talking about.
Nov 14 '05 #6

P: n/a
"MSReddy" <ms********@indiatimes.com> wrote:
"Ralmin" <ne**@ralminNOSPAM.cc> wrote:
"MSReddy" <ms********@indiatimes.com> wrote:
[...] Appologies for all the typos. As I said, the code I gave is
just an example. My structures are big . So the sizes that
I showed are different from sizes of struct A, B and C.
Unfortunately, I wouldnt be able to post my structures, so
I made up these structures (A,B and C) just give an example
of what I am talking about.


Do the structures you gave (A, B and C) exhibit the problem? If not, they
are not good examples. You should try to cut down the structures and the
program, until you get to the smallest possible amount of code that still
has the problem, then you should have a clearer view of what the problem
could be caused by. Once you have done that, if you still don't know, then
you can post the cut down code.

Here's all I can say at the moment:

The increase of eight bytes in struct C is as expected; the bool is of four
bytes, and requires alignment to a four-byte boundary. So, there are two
extra chars, two bytes of padding, then the bool which takes four bytes.

Your problem is the increase of 512 bytes in the size of struct A? And you
say that offsetof revealed that the increase is padding after the sc member
and before the a3 member?

I don't have an explanation of that... perhaps you need to provide more code
and/or more explanation.

--
Simon.
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.