c - Is a struct of pointers guaranteed to be represented without padding bits? -


I have a linked list that stores a set of settings for my application:

  typedef structure settings {straight settings * next; Four names; Four * title; Four * characters; Char * bkfolder; Char * srclist; Four * Arakol; Four * incfold; } Settings_row; Settings_row * first_profile = {0}; #defined SETTINGS_PER_ROW 7  

When I load prices in this structure, I do not want to name all the elements. I would like to treat it as a designated array - values ​​are loaded in sequence from a file and placed in the structure sequentially, then, when I need to use values, then I access them by name. I am

  // value to be incrementally inserted into straight void read_settings_file (settings_row * settings) {char * field = settings + sizeof (zero *); Int i = 0; While (read_value_into (field [i]) and amp; i ++ & lt; SETTINGS_PER_ROW); } // Name Settings by zero Settings_Info (settings_row * settings) {printf ("Settings 'Profile':% s \ n", settings.title); Printf ("Description:% s \ n", settings.desc); Printf ("Folder for backup:% s \ n", settings.bkfolder); }  

But I'm surprised, because these are all pointers (and can only be indicated in this structure), will the compiler add any padding from these values? Do they guarantee to be in this order, and there is nothing between values? Does my attitude sometimes work, but fails fiercely?

Edit for clarification

I realize that the compiler can pad any value of a structure - but the nature of the structure (the structure of a pointers) I thought it was a The problem may not be that since the most efficient way of 32 bit processor is in 32-bit sections, in this way compiler pad value (i.e., in a straight, int, short, ant, padding after small) Add 2 bytes Ega), to make it a piece of 32-bit, and the next 32 bits aligned next Int Chuck). But when a 32-bit processor uses 32-bit addresses (and 64-bit processors) uses 64-bit addresses (I think), then all values ​​of the structure (addresses, which are efficient by their very nature) Will be totally unnecessary, are the ideal 32-bit sections?

I hope some memory-representation / compiler-behavior master can be highlighted whether a compiler ever pad these values Cause will

< Under the POSIX rules, all pointers (both Function Pointer and Data Points) are all of the same size; div>

Only ISO C Under, all data pointers are variable for ' zero * ' and behind the loss of information without notice (but function pointer is changed to ' void * ' without loss Should not be information, nor vice versa).

Therefore, if properly written, Your code will work it's not written quite correctly, though! Opinion:

  void read_settings_file (settings_row * settings) {char * field = settings + sizeof (void *); Int i = 0; While (read_value_into (field [i]) and amp; i ++ & lt; SETTINGS_PER_ROW); }  

Assume that you are using a 32-bit machine with 8-bit characters; If you are using 64-bit machines then this is not all different arguments. Assignment is wrong for ' field ', because Settings + 4 '< There is an indicator for the 5th element (counting from 0) of an array of code> settings_row <'' structures what you have to write:

  zero read_settings_file (settings_row * settings) {Char * field = (char *) settings + sizeof (zero *); Int i = 0; While (read_value_into (field [i]) and amp; i ++ & lt; SETTINGS_PER_ROW); }  

Cast before extra is important!


C standard (ISO / IEC 98 99: 1999):

6.3.2.3 Points

An indicator to clear Indicator can be converted from an incomplete or object type. For an incomplete or object type, an indicator can be converted to zero and back in the pointer; The result will compare to the equivalent of the original pointer.

[...]

One type of function can be converted into an indicator as a second indicator again back; The result will compare to the equivalent of the original indicator. If a converted pointer is used to call a function whose type is not consistent with the indicated-to-type, the behavior is undefined.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -