Thread: please any one can solve my pragma problam

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    please any one can solve my pragma problam

    while putting prgma(2), pragma(4) i getting same result. any body can give explanation?

    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    The #pragma pack directive modifies the current alignment rule for members of structures following the directive.

    see the below link and u can google it man

    http://publib.boulder.ibm.com/infoce...f/rnpgpack.htm

    i dont think so on size #pragma will effect any thing

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

    can u check these programs

    Code:
    #include<stdio.h>
    #pragma pack(2)
    struct s
    {
    char ch1;
    char ch2;
    char ch3;
    char ch4;
    char ch5;
    char ch6;
    char ch7;
    };
    int main()
    {
    struct s s1;
    printf("%d\n",sizeof(s1));
    return 0;
    }

    putput :7


    Code:
    #include<stdio.h>
    #pragma pack(2)
    struct s
    {
    char ch1;
    char ch2;
    int i;
    char ch;
    };
    int main()
    {
    struct s s1;
    printf("%d\n",sizeof(s1));
    return 0;
    }
    output :8;



    why first program output 7, second program 8?

  4. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi rochy,

    can u check the below programs outputs

    Code:
    #include<stdio.h>
    #pragma pack(2)
    struct s
    {
    char ch1;
    char ch2;
    char ch3;
    char ch4;
    char ch5;
    char ch6;
    char ch7;
    };
    int main()
    {
    struct s s1;
    printf("%d\n",sizeof(s1));
    return 0;
    }

    output :7

    Code:
    #include<stdio.h>
    #pragma pack(2)
    struct s
    {
    char ch1;
    char ch2;
    int i;
    char ch7;
    };
    int main()
    {
    struct s s1;
    printf("%d\n",sizeof(s1));
    return 0;
    }
    output 8:


    please can u let me know ur suggestins , i little bit confusion how these bit will allign in memory

  5. #5
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Code:
    #include<stdio.h>
    #pragma pack(2)
    struct s
    {
    char ch1;
    char ch2;
    char ch3;
    char ch4;
    char ch5;
    char ch6;
    char ch7;
    };
    int main()
    {
    struct s s1;
    printf("%d\n",sizeof(s1));
    return 0;
    }

    output :7

    Code:
    #include<stdio.h>
    #pragma pack(2)
    struct s
    {
    char ch1;
    char ch2;
    int i;
    char ch7;
    };
    int main()
    {
    struct s s1;
    printf("%d\n",sizeof(s1));
    return 0;
    }
    output 8:


    please can u let me know ur suggestins , i little bit confusion how these bit will allign in memory
    hmm ..interesting it seems to allocate one extra space..when we store integer and char i tried it on double and integer..its not allocating extra space and the other data types

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    hey Nk Rao

    check this out may be u can get your answer, and u can visit this url dude

    sizeof - Wikipedia, the free encyclopedia

    Structure padding
    Main article: data structure alignment

    To calculate the sizes of user-defined types, the compiler takes into account any alignment space needed for complex user-defined data structures. This is why the size of a structure in C can be greater than the sum of the sizes of its members. For example, on many systems, the following code will print 8:
    Code:
    struct student{
      char grade; /* char is 1 byte long */
      int age; /* int is 4 bytes long */
    };
    printf("%u", (unsigned) sizeof (struct student));

    The reason for this is that most compilers, by default, align complex data-structures to a word alignment boundary. In addition, the individual members are also aligned to their respective alignment boundaries. By this logic, the structure student gets aligned on a word boundary and the variable age within the structure is aligned with the next word address. This is accomplished by way of the compiler inserting "padding" space between two members or to the end of the structure to satisfy alignment requirements. This padding is inserted to align age with a word boundary. (Most processors can fetch an aligned word faster than they can fetch a word value that straddles multiple words in memory, and some don't support the operation at all[1]).

    I think your all doubts can be cleared up now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please solve my #pragma pack problam
    By nkrao123@gmail. in forum C Programming
    Replies: 2
    Last Post: 10-10-2009, 03:22 AM
  2. labels inside the code #pragma mark label
    By nacho4d in forum C++ Programming
    Replies: 6
    Last Post: 01-11-2009, 02:50 AM
  3. Programming a PIC 16F84 microcontroller in C
    By Iahim in forum C Programming
    Replies: 1
    Last Post: 05-13-2004, 02:23 PM
  4. what does #pragma do?
    By cbc in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2003, 06:51 AM
  5. what is a pragma comment?
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 11-25-2002, 05:50 AM