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? 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? 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 ] 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.
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?
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.
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.
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.
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
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
jaysome wrote:
For good measure, include the following:
sizeof(char)
sizeof(signed char)
sizeof(unsigned char)
What's that all about?
--
pete
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
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.
>
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. 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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;
|
by: Kislay |
last post by:
case 1 :
struct s
{
char c1; // 8
double d; // 8
int i1; // 4
char c2; // 4
int i2; // 4
};
|
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...
|
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)...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
| |