473,811 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct padding

I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding?
Apr 25 '06 #1
9 4826
edware wrote:
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding?

Well, since the padding itself is compiler-dependant, the answer is
likely no.

Why do you care about padding?
Apr 25 '06 #2
edware wrote:
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding?


No.

Your compiler might have an option, if you hardware platform supports it.

--
Ian Collins.
Apr 25 '06 #3
Andrew Poelstra wrote:
edware wrote:
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding?

Well, since the padding itself is compiler-dependant, the answer is
likely no.

Why do you care about padding?


I care about it because it ruins things for me when I read
the whole structure with a fread() call. Is it better
to read and write each member of the struct individually?

Thanks for the help
Apr 25 '06 #4
edware <er***@hotmail. com> writes:
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding?


No. See question 2.12 in the comp.lang.c FAQ, <http://c-faq.com/>.

If the header in the file really contains a 2-byte magic number,
immediately followed by a 4-byte unsigned integer, the best approach
is probably to read it as an array of 6 bytes, then extract the bytes
you want, perhaps using memcpy(). You should also think about byte
ordering; is the size field stored high-order byte first or low-order
byte first?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 25 '06 #5
edware wrote:
Andrew Poelstra wrote:
edware wrote:
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding?

Well, since the padding itself is compiler-dependant, the answer is
likely no.

Why do you care about padding?


I care about it because it ruins things for me when I read
the whole structure with a fread() call. Is it better
to read and write each member of the struct individually?

Thanks for the help


Yes; that way when your struct changes, you won't have to recreate all
your files.

You should also use text files instead of binary for portability reasons.
Apr 25 '06 #6
Andrew Poelstra wrote:
edware wrote:
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
unsigned char would probably be better. char could be either signed or
unsigned.
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding? Well, since the padding itself is compiler-dependant, the answer is
likely no.


The answer is definitely no.

The best method is to read the data a byte at a time and reconstruct the
structures. This also avoids problems with endienness. For strict
portability you should be aware that a byte (and thus a char) may be
more than 8 bits!
Why do you care about padding?


He already provided that information!
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 26 '06 #7
edware <er***@hotmail. com> wrote:
Andrew Poelstra wrote:
edware wrote:
struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.
Why do you care about padding?


I care about it because it ruins things for me when I read
the whole structure with a fread() call. Is it better
to read and write each member of the struct individually?


Better, yes, since there's no portable way to guarantee that the layout
of a struct in memory matches that of a file record.
The best way to read binary records, though, is often to read the whole
thing into a buffer of unsigned chars using fread(), and then to copy
the values into the desired struct members using shifts and bitwise
operators. To write, do the opposite: use shifts and bitwise ops to move
values of a struct member into the required bytes of an unsigned char
array, and then write the whole thing using fwrite().

Richard
Apr 26 '06 #8
Richard Bos wrote:
edware <er***@hotmail. com> wrote:

Andrew Poelstra wrote:
edware wrote:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.
Why do you care about padding?


I care about it because it ruins things for me when I read
the whole structure with a fread() call. Is it better
to read and write each member of the struct individually?

Better, yes, since there's no portable way to guarantee that the layout
of a struct in memory matches that of a file record.
The best way to read binary records, though, is often to read the whole
thing into a buffer of unsigned chars using fread(), and then to copy
the values into the desired struct members using shifts and bitwise
operators. To write, do the opposite: use shifts and bitwise ops to move
values of a struct member into the required bytes of an unsigned char
array, and then write the whole thing using fwrite().


An added benefit of this approach is that it becomes
easier to deal with other issues affecting data exchange,
such as endianness, differing notions of how big an `int'
is, different floating-point representations , and so on.
Divorcing the external format from the internal format is
not just about avoiding alignment hassles, but has other
advantages as well.

--
Eric Sosman
es*****@acm-dot-org.invalid

Apr 26 '06 #9

"edware" <er***@hotmail. com> wrote in message
news:UK******** ***********@new sb.telia.net...
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right
things in the right place.

The struct starts like this:

struct header {
char magic[2]; /* 2 bytes */
uint32_t size; /* 4 bytes */
...
};

It seems like my compiler puts two extra
bytes after the magic field, which will
result in a wrong size value.

Is there some standard way to disable the padding?


As others have stated, there is no standard way to do this.
However, many compilers have #pragma directives or command
line switches to turn off the padding. While not strictly portable,
this might be a good choice to consider.

Ed
Apr 27 '06 #10

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

Similar topics

5
17662
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
5
2397
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The example below (from GBD's THE C BOOK) uses this. Is this a portable method? . /*
21
2545
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test this, and it seems to work fine, but are there any cases where doing something like this could cause any problems? Here's the small program I wrote to test this: #include <stdio.h>
8
1684
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the missing fields and arrays overlap. What's shown here, though, is sufficient for explaining the error. 290 bytes of data come from a serial device and is to be placed in this struct. Hence, I want this struct to be 290 bytes in size, and, if I'm...
6
1954
by: Christian Kandeler | last post by:
Hi, I'd like to know whether the following code is standard-compliant: struct generic { int x; int a; };
7
1551
by: venkatbo | last post by:
Hi folks, Of TurboGers & Django WAF candidates, which one would be easier to use in an environment where the data/content doesn't come an RDBMS, but from other server-side apps... If these are not good candidates, could you suggest appropriate ones... TIA, /venkat
2
2306
by: Michael Yanowitz | last post by:
Hello: I am relatively new to Python and this is my first post on this mailing list. I am confused as to why I am getting size differences in the following cases: >>> print struct.calcsize("I") 4
5
4054
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values before fwrite(), to shut up memory debuggers like Valgrind about writing uninitialized data to the file. Simplified code: static const struct S default_value = ...; struct S s, t;
11
3643
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a certain gradient, a struct "rounds up" to the nearest multiple of the gradient. This teacher is somewhat erratic and has said on subsequent days that first the padding is rounded up to nearest multiple of 4, and then the nearest power of 2.
3
3544
by: vikas talwar | last post by:
Hi All, Can you please explain me how the 'C' compiler allocate memory to 'struct'. Please go thu the example below and pls suggest me the solution for my problem. Here is my structure definition struct my_dev {
0
10393
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
10405
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
10136
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
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
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
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
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
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.