Midterm - Offset Question

18 views
Skip to first unread message

Amanda Nunez

unread,
Dec 10, 2009, 9:48:22 PM12/10/09
to utexas-cs352-fall2009
Hi guys,

I was re-doing #2 on the midterm and I wanted to know if these
answers sounded right:

var - offset
b - 0
q[7] - 8
w2 - 42
l - 38
x - 28
p - 16
r[3] - 23
r[-2] - 18 //this one i'm really unsure about
w - 32
f - 44

Also, are there more practice problems like this in the book? The only
one I found was super trivial in comparison.

Thanks,
Amanda

David L. Rager

unread,
Dec 10, 2009, 9:51:37 PM12/10/09
to Amanda Nunez, utexas-cs352-fall2009

David L. Rager

unread,
Dec 10, 2009, 9:52:44 PM12/10/09
to Amanda Nunez, utexas-cs352-fall2009
Hi,

I meant to preface that link.

This code should help:
http://groups.google.com/group/utexas-cs352-fall2009/browse_thread/thread/a8c8ee121e22f87b

David

On Thu, Dec 10, 2009 at 8:48 PM, Amanda Nunez <amanda...@gmail.com> wrote:

Bethany

unread,
Dec 10, 2009, 10:52:15 PM12/10/09
to utexas-cs352-fall2009
So, except for the first 2 i'm pretty sure those are all wrong. You
should try drawing it out. it really helps. Also, you should probably
read up on unions in the books if you have time. But i'll work it out
for you real quick cuz I know it's kinda the last minute. So b is at 0
and it's one byte long. since q is an array of char's (which are one
byte) it doesn't have to be aligned and starts at 1 with q[0] and goes
on to 10 where there's q[9]. Next you have to put in the union which
will need to be aligned to 4 bytes (cuz it has an int and a pointer)
and thus starts at 12. Union are not like structures, because their
elements share space in memory, therefore x, p, and r[0] all have the
same address which is 12. So r[-2] is at 12-2 which is 10. And r[3]
is at 12+3 which is 15.(also r[5] is at 12+5 which is 17 and thus the
union ends at 18) After that there's a very troublesome short
unsigned int, which you might think is 2 bytes, but I've been looking
into it and I'm pretty sure it's 4, someone correct me if i'm wrong.
So you align that to the 4 bytes and it goes at 20. Next the unsigned
long is also 4 bytes and goes at 24. Then you can put w2 at 28 and
it's only 2 bytes, however f is 8 bytes cuz it's a double and must be
aligned 4 so it goes to 32.
Important things to remember:
In linux alignment:
1 bytes things are not aligned (which is pretty much just char)
2 byte things are aligned to 2 bytes (which is pretty much just plain
shorts)
Anything bigger is aligned to 4 bytes (which is all pointers, ints,
longs, doubles)

And as far as I know, double and long double are the only things over
4 bytes being 8 and 10/12 respectively
(the chart of variable lengths is in the book on page 135, note that
nothing that says long but long double is over 4 bytes... which is
kinda confusing but not hard to remember)

Addison Denenberg

unread,
Dec 10, 2009, 10:57:06 PM12/10/09
to Bethany, utexas-cs352-fall2009
Message has been deleted

David L. Rager

unread,
Dec 10, 2009, 11:00:33 PM12/10/09
to Daniel Kuang, utexas-cs352-fall2009
To figure out the answer to this question, you should use the code I
sent out. Put a char after the union and look at that char's address.
If it's 6, then unions aren't word aligned. If it's 8, then unions
are at least word aligned (and possibly double-word aligned, but
they're not).

On Thu, Dec 10, 2009 at 9:58 PM, Daniel Kuang <dank...@gmail.com> wrote:
> I have another question on unions: So, if you have a union like
>
>  union {
>        int *p;
>        char r[6];
>        int x;
>  } our_union;
>
> Even though the biggest element is a 6-byte char array, the size of
> the union is 8 bytes?

Addison Denenberg

unread,
Dec 10, 2009, 11:21:20 PM12/10/09
to David L. Rager, Daniel Kuang, utexas-cs352-fall2009
I'm pretty sure unions use the alignment of whatever data type has the largest requirement. So in


 union {
       int *p;
       char r[6];
       int x;
 } our_union;

it would be aligned to a multiple of 4 because both int and int * need to be aligned to a multiple of 4.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages