Question: About bit-field storage.

Q

quemix

Hi all:
I have defined a structure (bit field) like this:

struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;

To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.
And when I use the same code to print an interger, it works well.

Here is my code and its output, can you give me some advice? Thank
you.

output of my code:
Print bit-field structure:
56 ffffff80 0 0
Print interger:
78 56 34 12

Code:

#include <stdio.h>

struct bit_test {
int a:4,
b:8,
c:20;
};

int main(void)
{
int i, testi;
struct bit_test test;
char *p;

test.a = 6;
test.b = 5;
test.c = 8;

p = (char *)&test;
printf("Print bit-field structure:\n");
for (i = 0; i < sizeof(struct bit_test); i++) {
printf("%x ", p);
}
printf("\n");


testi = 0x12345678;
p = (char *)&testi;
printf("Print interger:\n");
for (i = 0; i < sizeof(int); i++) {
printf("%x ", p);
}

printf("\r\n");
return 0;
}
 
C

Chris Dollin

quemix said:
Hi all:
I have defined a structure (bit field) like this:

struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;

To know the real memory storage of "test", I print every byte of it.

The "real memory storage" of bitfields is implementation-defined.
But surprisingly, the result is wrong: 56 ffffff80 0 0.

Why do you think this is wrong? Looks OK to me. Tinkering with your
code suggests that `a` is allocated in the low-order bits of a word,
`b` in the next up, and `c` in the remaining bits. At least on this
'ere x86 Linux box running gcc 4.1.1 ...
 
E

Eric Sosman

quemix wrote On 11/16/07 10:37,:
Hi all:
I have defined a structure (bit field) like this:

struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;

To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.

Since you do not know how `test' is laid out (that is
what you are trying to discover, after all), why do you
think the result is "wrong?"
And when I use the same code to print an interger, it works well.
[... code snipped ...]

Two suggestions:

1) Try the integer-printing code with a different value,
like 0x87654321.

2) Add `#include <limits.h>' at the beginning of your
program, and add `printf ("%d %d\n", CHAR_MIN, CHAR_MAX);'
somewhere within the executed part.
 
Q

quemix

Eric said:
Two suggestions:

1) Try the integer-printing code with a different value,
like 0x87654321.

2) Add `#include <limits.h>' at the beginning of your
program, and add `printf ("%d %d\n", CHAR_MIN, CHAR_MAX);'
somewhere within the executed part.

Thanks Chris and Eric.

When I replace char with unsigned char, only one byte is printed (no ffffff, which
is my purpose). I think the data in memory is same, and just the way to
interpret it changed.
 
B

Barry Schwarz

Hi all:
I have defined a structure (bit field) like this:

struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;

To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.
And when I use the same code to print an interger, it works well.

Here is my code and its output, can you give me some advice? Thank
you.

output of my code:
Print bit-field structure:
56 ffffff80 0 0
Print interger:
78 56 34 12

Code:

#include <stdio.h>

struct bit_test {
int a:4,
b:8,
c:20;
};

int main(void)
{
int i, testi;
struct bit_test test;
char *p;

Try unsigned char.
test.a = 6;
test.b = 5;
test.c = 8;

p = (char *)&test;

And here
printf("Print bit-field structure:\n");
for (i = 0; i < sizeof(struct bit_test); i++) {
printf("%x ", p);
}
printf("\n");


testi = 0x12345678;
p = (char *)&testi;


And here
printf("Print interger:\n");
for (i = 0; i < sizeof(int); i++) {
printf("%x ", p);
}

printf("\r\n");
return 0;
}



Remove del for email
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top