newbie: Packed bitspecs?

O

oiccicny

Is there a way to do the following code so that
day_data and other_data_1 occupy the same byte in
memory? Using the debugger, the addresses I'm getting are:
12ff5c - myVar.day_data
12ff60 - myVar.other_data_1
I'm using Microsoft Visual C++. sizeof(unsigned short) returns 2.
Thanks!
(e-mail address removed)

enum DAY {SUN,MON,TUE,WED,THUR,FRI,SAT};
class myClass
{
public:
DAY day_data : 3;
unsigned short other_data_1 : 5;
unsigned short other_data_2 : 8;
};
void main(void)
{
myClass myVar;
};
 
I

Ian Collins

Is there a way to do the following code so that
day_data and other_data_1 occupy the same byte in
memory?

Use a union. But they will occupy more than one byte.
enum DAY {SUN,MON,TUE,WED,THUR,FRI,SAT};
class myClass
{
public:
DAY day_data : 3;
unsigned short other_data_1 : 5;
unsigned short other_data_2 : 8;

Why are you using bit fields?
};
void main(void)

main returns int.
{
myClass myVar;
};
You don't need a semicolon after a function.
 
S

Sze

Is there a way to do the following code so that
day_data and other_data_1 occupy the same byte in
memory? Using the debugger, the addresses I'm getting are:
12ff5c - myVar.day_data
12ff60 - myVar.other_data_1
I'm using Microsoft Visual C++. sizeof(unsigned short) returns 2.
Thanks!
(e-mail address removed)

enum DAY {SUN,MON,TUE,WED,THUR,FRI,SAT};
class myClass
{
public:
DAY day_data : 3;
unsigned short other_data_1 : 5;
unsigned short other_data_2 : 8;};

void main(void)
{
myClass myVar;};


enum DAY {SUN,MON,TUE,WED,THUR,FRI,SAT};
class myClass
{
public:
struct day {DAY day_data : 3; int dummy:5;};
struct other {
unsigned short other_data_1 : 5;
int dummy : 3; // assure memory is managed how you want it to
be
unsigned short other_data_2 : 8;
};

union // anonymous union for easy access
{
day day;
other other;
};

};


int main(int argc, char *argv[]) // void works with most compilers but
it isn`t standard conform code,
{ // the parameters are the arguments
which were passed with the execution-call
myClass myVar;
myVar.day.day_data = MON;
...
};
 
J

James Kanze

Is there a way to do the following code so that
day_data and other_data_1 occupy the same byte in
memory? Using the debugger, the addresses I'm getting are:
12ff5c - myVar.day_data
12ff60 - myVar.other_data_1
I'm using Microsoft Visual C++. sizeof(unsigned short) returns 2.
enum DAY {SUN,MON,TUE,WED,THUR,FRI,SAT};
class myClass
{
public:
DAY day_data : 3;
unsigned short other_data_1 : 5;
unsigned short other_data_2 : 8;};

Not sure what you're trying to do, but bit fields don't have
addresses, so who knows what the debugger is showing you. And
how the compiler lays out bit fields is implementation defined,
so the above could be anything. If you're trying to match an
external format, you'll have to use shifting and masking.
 
J

James Kanze

Use a union. But they will occupy more than one byte.

Not necessarily.
Why are you using bit fields?

To save space? That's the only valid reason for them. (As
written, I would expect sizeof( myClass ) to be either 2 or 4 on
most wide spread machines. Without the bit fields, I would
expect 6. If you have a 100 million of them, it could make an
important difference.)
main returns int.

And C++ programmers don't put the void in the parentheses
either:).
You don't need a semicolon after a function.

In fact, it's illegal, and should result in a compiler error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top