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

Volatility of padding bytes?

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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?

Apr 22 '07 #1
14 1976
Fr************@googlemail.com a écrit :
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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?
Apr 22 '07 #2
Fr************@googlemail.com wrote:
>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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?
(2) Is not. A structure assignment could be implemented, for example,
using memcpy() internally.

Instead of making your code unreadable and hard to understand, why not
make the padding bytes 'visible' ?

For example, if your original struct was

struct s {
char c; /* assume 8 bits */
/* 1 byte padding here */
short s; /* assume 16 bits */
/* two bytes padding here */
long l; /* assume 32 bits */
};

(Typical for many 32 bit architectures)

rewrite it as:

struct s_new_and_improved {
char c;
char padding1;
short s;
char padding[2];
long l;
};

better yet, if you can rearrange the fields:

struct s_latest_and_greatest {
long l;
short s;
char c;
char padding[3];
};
Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Apr 22 '07 #3
Fr************@googlemail.com wrote:
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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?
Why don't you reorder the struct to eliminate the padding?

--
Ian Collins.
Apr 22 '07 #4
On Apr 22, 11:41 pm, jacob navia <j...@jacob.remcomp.frwrote:
Francine.Ne...@googlemail.com a écrit :
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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).
For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?
Did you mean to post a reply?

Apr 23 '07 #5
On Apr 22, 11:52 pm, Roberto Waltman <use...@rwaltman.netwrote:
Francine.Ne...@googlemail.com wrote:
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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).
For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?

(2) Is not. A structure assignment could be implemented, for example,
using memcpy() internally.

Instead of making your code unreadable and hard to understand, why not
make the padding bytes 'visible' ?

For example, if your original struct was

struct s {
char c; /* assume 8 bits */
/* 1 byte padding here */
short s; /* assume 16 bits */
/* two bytes padding here */
long l; /* assume 32 bits */
};

(Typical for many 32 bit architectures)

rewrite it as:

struct s_new_and_improved {
char c;
char padding1;
short s;
char padding[2];
long l;

};
Well, as I mentioned the chars I want to store in the padding bytes
don't have any relationship with the struct, so it seems to me it
would make the code quite confusing to include them as fields.

Apr 23 '07 #6
On Apr 22, 11:57 pm, Ian Collins <ian-n...@hotmail.comwrote:
Francine.Ne...@googlemail.com wrote:
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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).
For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?

Why don't you reorder the struct to eliminate the padding?
What if this is impossible? Or if the padding is at the end of the
struct?
--
Ian Collins.

Apr 23 '07 #7
Fr************@googlemail.com wrote:
On Apr 22, 11:57 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>Francine.Ne...@googlemail.com wrote:
>>>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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).
>>>For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?

Why don't you reorder the struct to eliminate the padding?

What if this is impossible? Or if the padding is at the end of the
struct?
Unlikely unless you have to match some external format and even if you
did, an external format is unlikely to have padding.

If the padding as at the end, it will be short.
>
*Please* don't quote signatures.
>
--
Ian Collins.
Apr 23 '07 #8
Ian Collins <ia******@hotmail.comwrote:
Fr************@googlemail.com wrote:
To conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars.
Why don't you reorder the struct to eliminate the padding?
Because that's not always possible, for various reasons.

Richard
Apr 23 '07 #9
On 22 Apr 2007 15:30:29 -0700, Fr************@googlemail.com wrote:
>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 conserve memory, I'd like if possible to use (after checking with
offsetof and sizeof that there's enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?
Your idea sounds tenuous at best and dangerously volatile at worst.
Forget about any concept of portability. You'd have a lot of
explaining to do if you worked for me and presented this idea (I'm
open to anything).

It would be best if you showed us your definition of your struct type
and also give us information on the sizes of the basic types for your
implementation and any typedefs you have defined that are used in your
struct definition, as well as the value of CHAR_BIT (a macro defined
in the standard header <limits.h>). Give us the results of at least
the following on your implementation, and any other types you happen
to use in your struct definition (e.g., sizeof(void*)):

sizeof(short)
sizeof(int)
sizeof(long)
sizeof(float)
sizeof(double)
sizeof(size_t)
CHAR_BIT

For good measure, include the following:

sizeof(char)
sizeof(signed char)
sizeof(unsigned char)

Regards
--
jay
Apr 23 '07 #10
jaysome wrote:
For good measure, include the following:

sizeof(char)
sizeof(signed char)
sizeof(unsigned char)
What's that all about?

--
pete
Apr 23 '07 #11
On Mon, 23 Apr 2007 10:03:19 GMT, pete <pf*****@mindspring.comwrote:
>jaysome wrote:
>For good measure, include the following:

sizeof(char)
sizeof(signed char)
sizeof(unsigned char)

What's that all about?
Keeping the honest people honest :)

--
jay
Apr 23 '07 #12
On Apr 23, 3:23 am, Francine.Ne...@googlemail.com wrote:
Well, as I mentioned the chars I want to store in the padding bytes
don't have any relationship with the struct, so it seems to me it
would make the code quite confusing to include them as fields.- Hide quoted text -
If the values have nothing to do with the structure what makes you
think including them in the padding is any less confusing than
including them as fields in the structure. Sounds to me like you might
want to rethink putting them in this structure at all.
Apr 23 '07 #13
>
For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
Yes. It can be done.
2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?
It can be guaranteed if you make sure that your code doesn't has any
structure assignments and memcpy( ) of the structure.

Please let me know if my understanding is wrong.

Apr 25 '07 #14
sm******@gmail.com wrote:
>For this to work, I'd need to know:
1) changing the padding bytes won't affect how the implementation
interprets the struct
Yes. It can be done.
>2) the implementation won't overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?
It can be guaranteed if you make sure that your code doesn't has any
structure assignments and memcpy( ) of the structure.

Please let me know if my understanding is wrong.
It's wrong.

"When a value is stored in an object of structure or
union type, including in a member object, the bytes
of the object representation that correspond to any
padding bytes take unspecified values."
-- ISO/IEC 9899:1999(E), section 6.2.6.1, paragraph 6

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 25 '07 #15

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

Similar topics

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...
28
by: ramu | last post by:
Hi all, I understand that some compilers pad some bytes to the aggregate data types in order to access the members of the aggregate data types(i.e. structure) fast. This depends on the...
20
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables...
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...
24
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
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...
33
by: Hahnemann | last post by:
Does anybody know the answer to the following? An unsigned short is 2 bytes long. Why is the following file created as 4 bytes instead of 2? file = fopen("data.bin", "wb"); if (file != NULL)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.