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

sizeof(myOwnObject) incorrect

Hey there

I have a problem with my own Objects. I have to write a programm in
school that read objects from a binary file.

This Object has the following structure:

#pragma once

class Sandbox
{
int test1; // 4 Byte
int test2; // 4 Byte
int test3; // 4 Byte
int test4; // 4 Byte
int test5; // 4 Byte
int test6; // 4 Byte
char name[25]; //25 Bytes
// 4 + 4 + 4 + 4 + 4 + 4 + 25 Bytes = 49 Bytes
public:
Sandbox(void);
~Sandbox(void);
};

There are no virtual methods ore anything like that. It's like i posted
above. My problem is, i expected sizeof(Sandbox) = 49, but it is 52 when
I do sizeof(Sandbox). There are 3 Bytes too much.

Any ideas where these 3 Bytes are and how I can remove them?

Sebastian

P.S. Sorry for my bad english, it's not my native language.
Nov 30 '07 #1
4 2124
Sebastian Fahr wrote:
I have a problem with my own Objects. I have to write a programm in
school that read objects from a binary file.

This Object has the following structure:

#pragma once

class Sandbox
{
int test1; // 4 Byte
int test2; // 4 Byte
int test3; // 4 Byte
int test4; // 4 Byte
int test5; // 4 Byte
int test6; // 4 Byte
char name[25]; //25 Bytes
// 4 + 4 + 4 + 4 + 4 + 4 + 25 Bytes = 49 Bytes
public:
Sandbox(void);
~Sandbox(void);
};

There are no virtual methods ore anything like that. It's like i
posted above. My problem is, i expected sizeof(Sandbox) = 49, but it
is 52 when I do sizeof(Sandbox). There are 3 Bytes too much.

Any ideas where these 3 Bytes are and how I can remove them?
It's called "padding". You may be able to remove them by using some
custom alignment instructions to your compiler. They are compiler-
specific, so the standard C++ language does not say what they are or
what they can be.

See if your compiler supports #pragma align, or any other similar
alignment command-line option or pragma. IOW, RTFM.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 30 '07 #2
On 30 Nov., 14:43, Sebastian Fahr <sebastian.f...@googlemail.com>
wrote:
Hey there

I have a problem with my own Objects. I have to write a programm in
school that read objects from a binary file.

This Object has the following structure:

#pragma once

class Sandbox
{
int test1; // 4 Byte
int test2; // 4 Byte
int test3; // 4 Byte
int test4; // 4 Byte
int test5; // 4 Byte
int test6; // 4 Byte
char name[25]; //25 Bytes
// 4 + 4 + 4 + 4 + 4 + 4 + 25 Bytes = 49 Bytes
public:
Sandbox(void);
~Sandbox(void);
You do not need the void here- its a C-ism.
>
};

There are no virtual methods ore anything like that. It's like i posted
above. My problem is, i expected sizeof(Sandbox) = 49, but it is 52 when
I do sizeof(Sandbox).
Your expectations were wrong. There is often extra padding in classes
- data that is unused. This data is there to assure proper performance
and/or operation of the computer as some data might have to be
properly aligned. Here, there is likely a requirement that an int is
aligned on a 4-byte boundary - an adress that is divisible by four.
The compiler thus adds three bytes to the class which means that you
can have your Sandbox class in an array and still adress e.g. the
second element.

/Peter
There are 3 Bytes too much.

Any ideas where these 3 Bytes are and how I can remove them?

Sebastian

P.S. Sorry for my bad english, it's not my native language.
Nov 30 '07 #3
Sebastian Fahr wrote:
I have a problem with my own Objects. I have to write a programm in
school that read objects from a binary file.
Unfortunately you cannot read binary data from a file *directly* into
a struct/class (eg. with a fread command). It's not portable and usually
just doesn't work (because of padding and word alignment reasons).

Usually hardware imposes some limitation on how a struct is organized
in memory. For example, if you have something like this:

struct A
{
int i1; // 4 bytes
char c; // 1 byte
int i2; // 4 bytes
};

you'll probably notice that in a typical 32-bit computer sizeof(A) will
be 12, and that 4 bytes will have been allocated for the char instead of
just 1 byte.

In some hardware this is for optimization (accessing non-word-aligned
integers causes penalties), in other hardware it's mandatory (trying to
access non-word-aligned integers causes a bus error interrupt).

If you had 9 bytes in a binary file and you assumed you could simply
read its content directly into that struct (eg. with fread), it would
just not work.
Nov 30 '07 #4

"Sebastian Fahr" <se************@googlemail.comwrote in message
news:fi**********@online.de...
Hey there

I have a problem with my own Objects. I have to write a programm in school
that read objects from a binary file.

This Object has the following structure:

#pragma once

class Sandbox
{
int test1; // 4 Byte
int test2; // 4 Byte
int test3; // 4 Byte
int test4; // 4 Byte
int test5; // 4 Byte
int test6; // 4 Byte
char name[25]; //25 Bytes
// 4 + 4 + 4 + 4 + 4 + 4 + 25 Bytes = 49 Bytes
public:
Sandbox(void);
~Sandbox(void);
};

There are no virtual methods ore anything like that. It's like i posted
above. My problem is, i expected sizeof(Sandbox) = 49, but it is 52 when I
do sizeof(Sandbox). There are 3 Bytes too much.

Any ideas where these 3 Bytes are and how I can remove them?

Sebastian

P.S. Sorry for my bad english, it's not my native language.
As other people are stating, it's from alignment issues. It appears your
compiler/os is 4 byte aligned for integers. Intergers need to start on a 4
byte boundary. Consider if you had the class:

class Foo
{
int MyInt;
char MyChar;
};

MyInt would be aligned to a 4 byte boundary, such as 4000 (hypothetical).
MyChar would then take up the next byte. So 4000 - 4003 would be MyInt,
MyChar would be in 4004. Now what if you had an array of 2 of these? Foo
Data[2]; Data[1] without padding would start on 4005. But 4005 is not 4
byte aligned. So the compiler adds padding bytes, unused bytes, at the end
of the array so in effect it becomes:

class Foo
{
int MyInt;
char MyChar;
char unused[3];
};

so now Data[1] would start on a 4 byte boundary. And that's what you're
seeing with sizeof.

There are a few ways to deal with this. The common way is serialization.
Read each data into each variable.
pseudo code since I don't use fread and dont' even know the syntax:

fread( myfile, MyInstance.test1, sizeof( MyInstance.test1 ) );
fread( myfile, MyInstance.test1, sizeof( MyInstance.test2 ) );
etc...
Read each part of the structure/class individually.

Another method that is sometimes used (but not encouraged) is to write and
read the same structure. So you'd write a Foo (with thepadding) and read it
back in but since the written structure had the padding so will the read and
it'll line up. This can work sometimes, but when you're getting a file from
an external source (such as you are) you can't really do that.

Yet another method is to kludge. Find out where the padding is, see if it
effects you and if it doesn't, read all but hte padding. Ugly, prone to
errors and crashes and a kludge, not a fix.

fread( myfile, MyInstance, sizeof( MyInstance ) - 3 );
or manually put the nubmer of bytes
fread( myfile, MyInstance, 49 );

This is ugly, and although you may be attempted to use it, your teacher
probably wouldn't accept it anwyay. What happens when you add another char
to the data? You're going to have to search your code for the "magic
numbers" 3 or 49 and figure out what they should be. :Don't do it.

Yet another way is some compilers have a #pragma or a command line switch to
turn packing off. Suce as one compiler uses:

#pragma pack(0)

saying don't use packing bytes. Although this can work, it is platform/OS
dependant and won't work on other platforms. Also, it can slow down some
computers as it has to fiddle intergers around in the CPU to line them up
right. Some CPUs will jsut plain crash when they try to use an unaligned
int.

So, what are your optoins? Well, I think I've listed them all, but the one I
would suggest is serialization.
Dec 1 '07 #5

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

Similar topics

3
by: Xiangliang Meng | last post by:
Hi, all. How does this 'sizeof(*area)' work? I think the reason is the sentence in C99: If the operand has type ¡®¡®pointer to type¡¯¡¯, the result has type ¡®¡®type¡¯¡¯, but I'm not sure. Could...
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...
18
by: sabarish | last post by:
Hi to one and all one of the developer asked me "Can u implement one user defined funtion similar to the sizeof operaor in C Programming Language". is it possible in C ?
5
by: Chris McDonald | last post by:
I've been trying to wean myself off using parentheses after the sizeof operator (and after the return keyword, too), but my understanding is challenged by the 4th use of sizeof in the following...
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
6
by: r.z. | last post by:
This piece of code crashes my app: basic_ifstream<TCHARfile(levelfilepath, ios_base::binary); unsigned int name_length; file.read( (TCHAR*)&name_length, sizeof(unsigned int) ); //crashes here -...
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
58
by: Nishu | last post by:
Hi All, When I run the below program in MSVC, I get the output as 1 4 Could you tell me why sizeof 'A' is taken as 4? Is it standard defined or compiler specific? Thanks, Nishu...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.