Structural Padding??

This is a discussion on Structural Padding?? within the C Programming forums, part of the General Programming Boards category; Dear All, I am writing program to avoid structural padding as follows using attributes but i am getting error in ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Structural Padding??

    Dear All,
    I am writing program to avoid structural padding as follows using attributes
    but i am getting error in this code...

    Code:
    #include "stdlib.h"
    int main()
    {
      struct data
      {
        int a;
        char ch;
        float s;
      }__attrubute__((packed));
    
      struct data e;
      printf("%u %u %u\n", &e.a, &e.ch,&e.s);
      printf("%d\n", sizeof(e));
      return 0;
      }

    errors are:-

    1. error C2065: 'packed' : undeclared identifier
    2. error C2440: 'initializing' : cannot convert from 'int' to 'struct main::data'

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    If it's like you other question, you're using the wrong compiler.

    > I am writing program to avoid structural padding as follows using attributes
    Why?

    Bearing in mind that most such reasons are ultimately flawed approaches to the problem.
    For example
    - mapping the header of a file such as BMP
    - mapping to a hardware register
    etc etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Microsoft compilers use #pragma pack if I'm not mistaken.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Yeah, GNU C compilers do no support #pragma. What you can do is

    Code:
     struct data
      {
        int a;
        char ch;
        float s;
      }__attrubute__((_packed_));
    Or you can work with your data directly within the struct, but you must know your compiler in how it will operate within the word boundary of your target system.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Or:
    Code:
     struct data
      {
        int a;
        char ch;
        float s;
      }
    #ifdef __GNUC__
        __attrubute__((_packed_));
    #elif defined(MSC)  /* not sure exactly what this would be */
        ...
    #else
        #error "Cannot eliminate structure padding"
    #endif
    ;
    Or something like that. (I think MSVC's #pragma has to go before the structure.)
    Last edited by dwks; 07-02-2008 at 11:28 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Code:
    #ifdef _MSC_VER
    #pragma pack(push, r1, 1)
    #endif
    struct data
    {
        int a;
        char ch;
        float s;
    }
    #ifdef __GNUC__
        __attrubute__((_packed_));
    #elif _MSC_VER
    #pragma pack(pop, r1)
    #else
        #error "Cannot eliminate structure padding"
    #endif
    ;
    I think that's how it's done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    #elif _MSC_VER
    Bad idea -- you'll get an error if _MSC_VER is not defined.
    Code:
    #elif defined(_MSC_VER)
    is better, or, if you're really testing the truth of _MSC_VER (which I doubt you want),
    Code:
    #elif defined(_MSC_VER) && _MSC_VER
    Other than that . . . it looks like it might work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Ah, of course. But _MSC_VER is guaranteed to be defined if it's a Microsoft compiler so you only really need to check if it's defined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What if it's a non-MS (and non-GCC) compiler? You'll get an error like "_MSC_VER not defined" instead of my nice #error.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    BTW: this works on GCC. That is, it prints 2. (Care to test it on MSVC?)
    Code:
    #ifdef _MSC_VER
    #pragma pack(push, r1, 1)
    #endif
    struct data
    {
        char one, two;
    }
    #ifdef __GNUC__
        __attribute__((__packed__));
    #elif defined(_MSC_VER)
    #pragma pack(pop, r1)
    #else
        #error "Cannot eliminate structure padding"
    #endif
    ;
    
    #include <stdio.h>
    
    int main() {
        printf("&#37;d\n", (int)sizeof(struct data));
        return 0;
    }
    Note how I had to change the GCC line:
    Code:
        __attribute__((__packed__));
    attribute and double underscores surrounding packed.

    [edit] It's kind of funny how many times that buggy code was copy-and-pasted without anyone actually reading it . . . . [edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    It prints 2 on msvc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    For some reason, it prints 2 even when I enable structure padding (with GCC). I guess structure padding doesn't apply to structures consisting entirely of chars, on this system at least.

    So here's another one to test if you like . . . on my 64-bit system, it prints 16 with structure padding enabled and 9 with it disabled.
    Code:
    #ifdef _MSC_VER
    #pragma pack(push, r1, 1)
    #endif
    struct data
    {
        char one;
        double two;
    }
    #ifdef __GNUC__
        __attribute__((__packed__))
    #elif defined(_MSC_VER)
    #pragma pack(pop, r1)
    #else
        #error "Cannot eliminate structure padding"
    #endif
    ;
    
    #include <stdio.h>
    
    int main() {
        printf("&#37;d\n", (int)sizeof(struct data));
        return 0;
    }
    There's another change, too: the semicolon was removed from the GCC declaration, to remove this warning:
    Code:
    structpad.c:16: warning: ISO C does not allow extra ‘;’ outside of a function
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by dwks View Post
    So here's another one to test if you like . . . on my 64-bit system, it prints 16 with structure padding enabled and 9 with it disabled.
    Yup, same here, even on a 32-bit system.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Right, it would depend on the size of words and of a double. Just thought I'd mention it.

    So there you have it: revision 4 or whatever of our GCC+MSVC non-padded structure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange struct padding?
    By cyberfish in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2011, 09:42 PM
  2. Structure Padding in RISC and CISC
    By karthik537 in forum C Programming
    Replies: 3
    Last Post: 01-20-2009, 02:12 AM
  3. string padding and replacement functions
    By George2 in forum Tech Board
    Replies: 4
    Last Post: 11-19-2006, 12:40 AM
  4. Prob with padding in BMP image files
    By tin in forum Game Programming
    Replies: 2
    Last Post: 01-09-2006, 07:23 AM
  5. Structure Padding, pragma pack...
    By P.Phant in forum C Programming
    Replies: 4
    Last Post: 06-04-2003, 05:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21