473,395 Members | 1,915 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,395 software developers and data experts.

Size of Structure

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
6 2160

"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

"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
>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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
18
by: Anand Buddhdev | last post by:
Hi everyone, I'm a C newbie, so please be gentle. I have a program that defines the following things: typedef union { unsigned int I; unsigned char b; } dword;
7
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be...
4
by: Shayne H | last post by:
How can I create a structure that gets passed to an API call that will automatically set its size field? I tried the following: <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
1
by: Abdul Samad | last post by:
Assalam mu Alikum, i understand the size of structure with no data types is zero. but why the size of structure variable is 1 Byte when the size of that structure is zeor? for example ...
15
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
1
by: =?Utf-8?B?VGhvbWFzUg==?= | last post by:
Hi together, I have following little problem with a structure which I need for unmanaged code: Public Structure Info Public Header As HeaderInfo ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.