Thread: please solve my #pragma pack problam

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    please solve my #pragma pack problam

    i getting the same solution for below program while changing the pack value in pragma

    Code:
    #include<stdio.h>
    #pragma pack(2)
    struct hai
    {
    char ch1;
    char ch2;
    char ch3;
    char ch4;
    char ch5;
    }s;
    int main()
    {
    printf("%d \n",sizeof(s));
    return 0;
    }
    i getting output as :6 in both case of prgma pack vaues 2 and 4.

    why i getting same values , please any body clear my confusion

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What compiler are you using? What size are you expecting? 6 would indicate that it is aligning at 2 bytes (what you have up in your pragma). If you are using GCC, you'll need something like this:
    Code:
    #define PACK1 __attribute__((aligned (1),packed))
    
    typedef struct something {
            int a, b;
            char c, d;
    } PACK1 SOMETHING;
    Now the sizeof (SOMETHING) would be 10.

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    hi canady,

    i am using RHEL5 GCC 4.1.1 compiler. my mechine is x86 based pc. i think generally it will take 4 bytes everytime.

    first time it will take four bytes .so
    i expecting 8 in prgma pack 4


    1.in case of #pragma pack 2

    first 4 char variables will take 4 bytes.second char will take two bytes for pack 2.so totally 6 byes

    2.in case of #pragma pack 4

    first 4 char variables will take 4 bytes and next char will go to anoter pack ie second pak with 4 bytes. but character will take 1 bytes. but output caounts 6 bytes .

    i expecting 8 bytes in pragma 4.

    why it taking 2 bytes onlly in pragma 2 and prgma 4

    i look for your further replys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-03-2005, 01:03 AM
  2. Programming a PIC 16F84 microcontroller in C
    By Iahim in forum C Programming
    Replies: 1
    Last Post: 05-13-2004, 02:23 PM
  3. Structure Padding, pragma pack...
    By P.Phant in forum C Programming
    Replies: 4
    Last Post: 06-04-2003, 05:56 AM
  4. To service pack or not to service pack
    By Waldo2k2 in forum Tech Board
    Replies: 17
    Last Post: 01-08-2003, 05:35 AM
  5. what is a pragma comment?
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 11-25-2002, 05:50 AM