473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2183

"MSReddy" <ms********@ind iatimes.com> a écrit dans le message de
news:67******** *************** ***@posting.goo gle.com...
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********@ind iatimes.com> a écrit dans le message de
news:67******** *************** ***@posting.goo gle.com...
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********@ind iatimes.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**@ralminNOS PAM.cc> wrote in message news:<15******* *************** ********@news.t eranews.com>...
"MSReddy" <ms********@ind iatimes.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********@ind iatimes.com> wrote:
"Ralmin" <ne**@ralminNOS PAM.cc> wrote:
"MSReddy" <ms********@ind iatimes.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
2465
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 nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
18
1813
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
2186
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 the size of "ethernet_frame" struct and why ? typedef struct { //This struct defines Arp--Request.
4
1921
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)> _ Public Structure OsVersionInfo Shared Sub New() Size = Marshal.SizeOf(GetType(OsVersionInfo)) End Sub Public Shared ReadOnly Size As Integer
6
5019
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 class MyClass {
8
10172
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
1
2065
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 struct student{ } st; here structure student has zero size but why the size of st is not zero? why it is one?
15
2241
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
4808
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
3030
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 <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)_ Public Notes As Integer()
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.