473,396 Members | 2,039 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,396 software developers and data experts.

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 4814
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_Keith) 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*******************@newsb.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
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...
5
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...
21
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...
8
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...
6
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
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...
2
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...
5
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...
11
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...
3
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.