CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    38

    Regarding structure size

    #include <iostream.h>
    hi,

    I want to use structure for reprentation of bits and do not want
    maximum size of integer.

    here I have given the code and it is giving out put for size of structure as 4 bytes.

    but how can i get exact size what i am using.


    struct packet
    {
    int FC :8;
    int DS :4;
    };

    int main()
    {
    packet pack;

    cout<<"size ofpacket"<<sizeof(pack)<<endl;
    pack.FC=124;
    pack.DS=15;
    cout<<"pack.FC="<<pack.FC<<endl;
    cout<<"pack.DS="<<pack.DS<<endl;
    cout<<sizeof(packet);

    return 0;
    }


    please hel p me..

    bye
    seshu
    make it possible

  2. #2
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    For setting an alligment of structure in memory use:
    #pragma pack(push, _size_)
    struct name_t
    {
    ...
    };
    ...
    #pragma pack(pop)

    where _size_ is a const value of alligment in bytes.
    So in your case use _size_=2
    Last edited by dimm_coder; April 23rd, 2003 at 08:09 AM.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    or you could try:
    Code:
    struct packet
    {
        char FC : 8;
        char DS : 4;
    };
    You won't get the size less than 2, though (assuming 8-bit bytes).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Originally posted by Graham
    or you could try:
    Code:
    struct packet
    {
        char FC : 8;
        char DS : 4;
    };
    You won't get the size less than 2, though (assuming 8-bit bytes).
    Even in this case U need to use #pragma pack () , because default system aligment for some compiler can have a some fixed value. Default ussually = 4 bytes. So every struct will be rounded to 4-bytes boundary by default.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    True, true.......
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    38
    thank you..

    as u told depends on compiler size of data type varies.

    bye
    seshu
    make it possible

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured