Application of Union in C

P

pragma

humm, thank you regis. I was wrong about the equal sign, posed that in
general the padding will be always there.

---mauricio
 
N

Naresh

And about the sequence
char, int, long, double..

char (3 padding), int (2 padding if sizeof (int) == 2), long, double ==
20

but what i'm getting is 24. What could be the reason?


#include<stdio.h>

typedef union
{
int int_member;
long long_member;
double double_member;

} cool_union;

typedef struct
{
char char_member;
int int_member;
long long_member;
double double_member;

} cool_struct;

int main (void)
{
cool_union x;
cool_struct y;

printf("%d %d %d %d %d %d\n",sizeof(char), sizeof(int),
sizeof(long),
sizeof(double), sizeof(cool_union), sizeof(cool_struct));

return 0;

}

output is

1 4 4 8 8 24

I thought we were considering here word(4 bytes) align allocation for
the members.. but it doesnt look like that. Please guide.
 
N

Naresh

Naresh said:
And about the sequence
char, int, long, double..

char (3 padding), int (2 padding if sizeof (int) == 2), long, double ==
20

but what i'm getting is 24. What could be the reason?

Okk, i understood. Since after allocation to long, it points to word
align byte and the next element takes double word(8 bytes). So the
padding of one more word (4 bytes) is required to make it double word
align and hence the sum becomes 1+ (3) + 4 + 4 + (4) + 8 = 24.
I hope this is the solution.
 
R

regis

Naresh said:
Okk, i understood. Since after allocation to long, it points to word
align byte and the next element takes double word(8 bytes). So the
padding of one more word (4 bytes) is required to make it double word
align and hence the sum becomes 1+ (3) + 4 + 4 + (4) + 8 = 24.
I hope this is the solution.

to confirm or infirm this, compute the internal padding:

struct cool_struct s;
size_t offset1= (char*)& s.long_member -(char*)& s;
size_t offset2= (char*)& s.double_member-(char*)& s;
size_t padding= offset2 - (offset1 + sizeof s.long_member);

or use the macro offsetof(type,membername)
instead of pointer arithmetic.
 
K

Kevin Handy

Abdo said:
Any logical reason for this? I always use floats, especially if I don't
need the extra precision of double, also many third part libraries
(Like DirectX and OpenGL) use them...


On many machines, float is slower than double.
This is not true of all CPU's/OS's, and sometimes
floats actually are faster, but on the average
doubles are as fast or faster than floats.

This is because, on those machines, all the math
routines are written to handle doubles, and float's
need to be converted to double to pass into the math
functions, and then the result needs to be converted
back to a float. By using double, you skip two
or three conversions.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top