|
-
April 23rd, 2003, 07:50 AM
#1
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
-
April 23rd, 2003, 08:07 AM
#2
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!"
-
April 23rd, 2003, 08:12 AM
#3
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
-
April 23rd, 2003, 08:19 AM
#4
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!"
-
April 23rd, 2003, 09:33 AM
#5
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
-
April 23rd, 2003, 09:40 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|