Size of Structure

M

MSReddy

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
 
R

Régis Troadec

MSReddy said:
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
 
R

Régis Troadec

MSReddy said:
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
 
G

Gordon Burditt

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
 
R

Ralmin

MSReddy said:
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.

[...]

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

[...]

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;
}
 
M

MSReddy

Ralmin said:
MSReddy said:
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.

[...]

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

[...]

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

Ralmin

MSReddy said:
Ralmin said:
[...]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top