Thread: How to handle structure padding

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    How to handle structure padding

    I don't have a clear example for this. Say I had something like:

    Code:
    #include <stdio.h>
    
    struct packet {
     int head;
     char tail;
    };
    
    int main(void) {
    
    struct packet route;
    
    return 0;
    }
    I know if I compile the code on Suse Linux 9.1 and then on FreeBSD 4.6, the structure would get padded differently. Would using memcpy() be a possible solution to get around the padding problem?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cdalten
    Would using memcpy() be a possible solution to get around the padding problem?
    What problem?

    [edit]http://c-faq.com/struct/padding.html
    Last edited by Dave_Sinkula; 03-17-2006 at 10:27 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think the OP is trying to do something with the structure that depends on the structure having the same padding, such as reading or writing the structure with fread() or fwrite().
    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.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    I was seeing if memcpy() would work when reading from the Unix Process Accounting Structure. Ie

    Code:
    typdef u_short comp_t;
    
    struct acct {
     char ac_flag;
     char ac_stat;
    
     uid_t ac_uid;
     gid_t ac_gid;
     dev_t tc_tty:
     time_t ac_btime;
     comp_t ac_utime;
     comp_t ac_stime;
     comp_t ac_etime;
     comp_t ac_mem;
     comp_t ac_io;
    
     comp_t ac_rw;
     char ac_comm[8];
    };
    Instead doing an fread() and risk alignment issues from one platform to another, could I use memcpy() to bypass it?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't memcpy directly from disk. memcpy is for manipulating stuff loaded into memory.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by quzah
    You don't memcpy directly from disk. memcpy is for manipulating stuff loaded into memory.


    Quzah.
    OKay, can you or someone clarify that stateent a bit.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I thought that was pretty straighforward.

    fread (essentially) grabs a bag of bytes from a file and puts it somewhere in memory.
    memcpy grabs a bag of bytes from one place in memory and puts it somewhere else in memory.

    So memcpy doesn't read from a file.


    Perhaps conditionally compile with appropriate pack directives for each compiler? Serialize and use a text file instead?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay. That makes sense.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    most compilers accept:

    Code:
    /* align on 1 byte boundaries (no padding) */
    #pragma pack(push, 1)
    struct X { };
    #pragma pack(pop)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure packing
    By rajkumarmadhani in forum C Programming
    Replies: 3
    Last Post: 11-26-2007, 03:44 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM