AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
 View Only
  • 1.  sizeof (

    Posted Sun May 08, 2011 07:24 AM

    Originally posted by: SystemAdmin


    > uname -a
    AIX ep5512b 1 6 000497A2D900

    > xlC -qversion
    IBM XL C/C++ for AIX, V10.1
    Version: 10.01.0000.0000
    Hi

    //
    foo.cpp
    #include <iostream>
    struct Foo
    {
    unsigned char m1:1;
    unsigned char m2:7;
    };

    int main()
    {
    std::cout << sizeof(Foo) << std::endl;
    return 0;
    }
    //
    > xlC foo.cpp
    // No errors

    > ./a.out
    4 // not 1
    // ==========
    sizeof(Foo) on most of compilers is 1.
    Do we have a way to say to compiler that sizeof(Foo) should be 1?

    Thanks

    Alex
    #AIX-Forum


  • 2.  Re: sizeof (

    Posted Mon May 09, 2011 05:40 AM

    Originally posted by: shyhc


    Probably not. I guess even
    
    struct Foo 
    { unsigned 
    
    char m1; 
    };
    

    would show up as 4 due to alignment reasons,
    but anyway the compiler will treat the bitvector
    as something longer no matter how many bits you allocate.
    But there may be better forums for this...
    #AIX-Forum


  • 3.  Re: sizeof (

    Posted Mon May 09, 2011 12:22 PM

    Originally posted by: TRB


    I agree with shyhc, the reason has to do with allignment.

    However, assuming you are using the AIX compiler, and further
    assuming you NEED to sizeof to return 1, you can use the "-qalign=packed"
    argument to the compiler which will change the alignment rules for
    the whole program, or you can use a "pragma" to change the alignment
    rules for a single structure.

    Example of pragma:

    #pragma options align=packed
    struct Foo
    {
    unsigned char m1;
    };
    #pragma options align=reset

    HTH,
    -tony
    #AIX-Forum


  • 4.  Re: sizeof (

    Posted Wed May 11, 2011 12:03 AM

    Originally posted by: SystemAdmin


    Thanks

    //
    #include <iostream>

    struct Foo0
    {
    unsigned char m1;
    };

    struct Foo1
    {
    unsigned char m1:1;
    };
    #pragma options align=bit_packed
    struct Foo2
    {
    unsigned char m1:1;
    };
    #pragma options align=reset

    int main()
    {
    std::cout << sizeof(Foo0) << std::endl;
    std::cout << sizeof(Foo1) << std::endl;
    std::cout << sizeof(Foo2) << std::endl;

    return 0;
    }
    //

    > ./a.out
    1
    4
    1
    #AIX-Forum