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

padding bits...

Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?

For example let's say char is 1 byte and int is 4 byte in this case
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?

copx
Nov 14 '05 #1
8 1591
copx wrote:

Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?
Yes.
For example let's say char is 1 byte
It is, by definition. But that byte may have more than 8 bits. The
number of bits making up a byte (and consequently the "width" of
a char) is CHAR_BIT, which must be >= 8.
and int is 4 byte in this case
Okay, for the sake of argument. (Of course, it might not be, but
it sounds like you know that already.)
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?


No. The implementation may put an arbitrary number of padding bytes
after any (or even all!) of those members.
Nov 14 '05 #2

copx wrote:
Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?

For example let's say char is 1 byte and int is 4 byte in this case
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?


It isn't. Depends on the platform. First off "int is 8 octets" on my
platform and yeah it could be padded.

If you're thinking of read/write'ing this verbatim to file ... don't.

Tom

Nov 14 '05 #3
Tom St Denis schrieb:
copx wrote:
Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?

For example let's say char is 1 byte and int is 4 byte in this case
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?

It isn't. Depends on the platform. First off "int is 8 octets" on my
platform and yeah it could be padded.

If you're thinking of read/write'ing this verbatim to file ... don't.


Yep, that was the idea.

Thanks to everyone who replied,
copx
Nov 14 '05 #4
In message <d0*************@news.t-online.com>
copx <in*****@invalid.com> wrote:
Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?

For example let's say char is 1 byte and int is 4 byte in this case
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?


It's definitely not guaranteed. But in this particular case, the vast
majority of real-world implementations with 32-bit ints would have the
structure as 64 bytes long. An awful lot of real-world portable code relies
on cases like that (particularly from BSD Unix), so an implementation that
wilfully added padding would be unlikely to be accepted by the market.

A more likely implementation difference will be the endianness of the ints.
BSD code tends to rely on the structure layout for file/network interchange
but uses endianness swap functions to sort out shorts and ints.

It's safer if you wrote data structures out to disc manually, one byte at a
time. A handy subroutine would be a "fput_int32" function that outputs a
32-bit number as 4 bytes.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #5
Kevin Bracey <ke**********@tematic.com> wrote:
In message <d0*************@news.t-online.com>
copx <in*****@invalid.com> wrote:
Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?
Yes.
For example let's say char is 1 byte and int is 4 byte in this case
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?


It's definitely not guaranteed. But in this particular case, the vast
majority of real-world implementations with 32-bit ints would have the
structure as 64 bytes long.


Conversely, though, if file_name were 55 chars rather than 56, there
would almost certainly (but again, not guaranteed) be a padding byte
between it and file_position, and the size of the struct would probably
still be 64 bytes.

Richard
Nov 14 '05 #6


copx wrote:
Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?

For example let's say char is 1 byte and int is 4 byte in this case
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?

copx


The compiler may add bytes to align the data with what ever the CPU
likes best. 1,2,4,or 8 bytes
Odd spacing would slow down the memory fetches. If you need specific
packing Look up the non-standard structure packing options for you
compiler.
Nov 14 '05 #7
Neil Kurzman wrote:
copx wrote:
Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?


The compiler may add bytes to align the data with what ever the
CPU likes best. 1,2,4,or 8 bytes


Compilers can add padding for reasons other than individual member
alignment requirements.

--
Peter

Nov 14 '05 #8
>Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?
Yes.

For example let's say char is 1 byte
In C, this is true by definition, but if you meant char is one Disk
Manufacturer's Byte, that might also be true.
and int is 4 byte in this case
It might be, but there are plenty of exceptions to this, and there
will probably be a growing number where int is 8 Disk Manufacturer's
Bytes.
and I've a struct defined like this:

struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};

Is the size of this struct guaranteed to be 64 bytes
in this case or not?


The size of a structure is never guaranteed to be exactly anything and
it is not guaranteed that one copy of it will fit on your hard disk
(if your system has one). Yes, it could have 700GBytes of padding.
Not that such an implementation would sell well.

If you are considering moving such a file across systems, consider
converting it to a text format instead:

fprintf(pak, "%ld %ld %s\n", pdep->file_position, pdep->file_length,
pdep->file_name);

Assuming, of course, that legal file names can't contain newlines (something
not guaranteed on UNIX systems).

Gordon L. Burditt
Nov 14 '05 #9

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

Similar topics

13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
16
by: s.seitz | last post by:
hi gods and programmers! i've got a weird problem with strlen() in C. in a current project, i need to AES_decrypt() some string. By now, i'm using the libssl (openssl) API. To get this...
2
by: manochavishal | last post by:
Hi, In standard i have come across: 6.2.6 Representations of types 6.2.6.1 General 6.When a value is stored in an object of structure or union type, including in a member object, the...
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...
14
by: Francine.Neary | last post by:
Consider the following situation. I have a large static array s of structs, say of size 500. I also need a smaller array of chars, say of size 100, which has nothing to do with the struct. To...
6
by: john | last post by:
Many times I have seen padding bits being mentioned here. What is the purpose of the padding bits?
6
by: John Messenger | last post by:
I notice that the C standard allows padding bits in both unsigned and signed integer types. Does anyone know of any real-world examples of compilers that use padding bits? -- John
4
by: Ioannis Vranos | last post by:
Under C95: Is it guaranteed that char, unsigned char, signed char have no padding bits?
3
by: vippstar | last post by:
Hey comp.lang.c I'm somewhat confused with bit padding. I tried searching the FAQ, but there isn't a search feature, so I used google and the search query: site:c-faq.com padding. I did not...
2
by: Mihai Rusu | last post by:
Hello I am trying to figure out if the standard allows integral type representations with padding (ie value representation != object representation). A conforming implementation can have: - a...
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: 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
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:
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
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,...

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.