Test 2 :: Number 2

6 views
Skip to first unread message

HM

unread,
Nov 16, 2009, 11:06:53 PM11/16/09
to utexas-cs352-fall2009
Can someone please verify or fix the answers for question 2 from Test
2. THESE MAY BE WRONG!

b 0 p 12
q[7] 8 r[3] 15
w2 28 r[-2] 10
l 24 w 20
x 12 f 32

Addison Denenberg

unread,
Nov 16, 2009, 11:14:43 PM11/16/09
to HM, utexas-cs352-fall2009
I'm not sure how you got all the way to 32 for f but I do know that the size of a union equals the size of the largest element in the union, which in this case would be char r[6] so w should be at 17 I think. And why exactly does the union start at 12? I'm pretty sure thats right because I put 11 on the test and got it wrong but I'm not sure why. Does the first byte contain the length of the array?

J R

unread,
Nov 16, 2009, 11:32:11 PM11/16/09
to utexas-cs352-fall2009
> b 0 p ?
> q[7] ? r[3] 19
> w2 ? r[-2] 14
> l 24 w ?
> x ? f 32
Those are the ones I got correct, now that is also assuming when he
said he graded lenient. I believe he said he graded upon if you got
something wrong, he tried to give as much points to figure out how you
got so and so wrong. I was under the impression for this problem you
needed to make sure everything was a multiple of 4.

0-1____4_______14__16_______22__24_______28__30__32_________
b |------| q[10] |----| our_union | w | L | w2|
----| f

So my in space memory looked like that, which was wrong but maybe some
of it was right,.. still going through this one.

On Nov 16, 10:14 pm, Addison Denenberg <addison.denenb...@gmail.com>
wrote:
> I'm not sure how you got all the way to 32 for f but I do know that the size
> of a union equals the size of the largest element in the union, which in
> this case would be char r[6] so w should be at 17 I think. And why exactly
> does the union start at 12? I'm pretty sure thats right because I put 11 on
> the test and got it wrong but I'm not sure why. Does the first byte contain
> the length of the array?
>

J R

unread,
Nov 16, 2009, 11:38:39 PM11/16/09
to utexas-cs352-fall2009
I meant to say a multiple of some k. (2,4, or 8). I read this in chp
3.10 pg 198.

Addison Denenberg

unread,
Nov 17, 2009, 12:08:21 AM11/17/09
to J R, utexas-cs352-fall2009
Ok, so I think I figured it out, hopefully someone who got it all right can verify

b = 0               p = 12
q[7] = 8           r[3] = 15
w2 = 24           r[-2] = 10 
l = 20              w = 18
x = 12             f = 32

Ok first off, JR on yours, and correct me if I'm wrong, I think you got your values for r[3] and r[-2] correct because you chose 16 for the starting address for the union and like you said, he tried to give partial credit if you got some of it wrong but part of it right because I think the correct answer for the beginning of the union is 12 since the strictest elements in the union are int and int * which have the same alignment rule of 4. As far as I can tell since char is only 1 byte it doesn't matter what address the q[] starts at so 1 should be ok. So q takes up addresses 1 - 10 and the union takes up 12 - 17 and from there its pretty easy to finish up the rest. Shorts are 2 bytes so their address needs to be a multiple of 2 so our next address is 18 which is a multiple of 2 and the short will take up 18 and 19, then the unsigned long is 4 bytes and our next address is 20, which is a multiple of 4, so it will take up 20 - 23, then another short and the next address is 24 so we don't need to adjust and it will take up 24 and 25 and last is the double which takes up 8 bytes so it needs to start at a multiple of 8 and the next address that is a multiple of 8 would be 32 and it will take up addresses 32 - 39.

I hope I was able to explain it well. All of this makes sense to me from what I have read but hopefully someone (possibly David) can confirm if this is right or wrong.

Let me know what you guys think
Addison

Addison Denenberg

unread,
Nov 17, 2009, 12:13:43 AM11/17/09
to J R, utexas-cs352-fall2009
Oh, I forgot to explain the union. I read that in a union since the size of it is the size of the largest element in it, it can only hold one piece of data at a time. So in this case, it can hold data for either the array, the int, or the int * but not all 3 at once. Now that you know that (in case you didn't) if its an int or an int * it will point to the first address of the structure but if it is the array, then it will be accessed like a normal array. So r[0] = 12, r[1] = 13 and so on

HM

unread,
Nov 17, 2009, 12:22:37 AM11/17/09
to utexas-cs352-fall2009
Wouldnt r[6] = 18 ? If thats true then w would have to be 20

On Nov 16, 11:13 pm, Addison Denenberg <addison.denenb...@gmail.com>
wrote:

David L. Rager

unread,
Nov 17, 2009, 12:26:21 AM11/17/09
to HM, utexas-cs352-fall2009
Ya'll should just run the C code I sent out in this thread. It's
setup to give you the answers to the test question.

http://groups.google.com/group/utexas-cs352-fall2009/browse_thread/thread/a8c8ee121e22f87b

I wouldn't swear to them, but off the top of my head, these look familiar.

Addison Denenberg

unread,
Nov 17, 2009, 12:48:10 AM11/17/09
to David L. Rager, HM, utexas-cs352-fall2009
I think this has something to do with the unsigned and int in the declaration along with short. I printed out the size of w when I ran it and the size is 4 so now all of those addresses make sense and why w is at 20 and not 18. So I think maybe the unsigned and int in the declaration override the short somehow.

Addison

Keith Ribe

unread,
Nov 17, 2009, 1:01:41 AM11/17/09
to utexas-cs352-fall2009
I agree. Which would make the first post of this thread correct:

b 0 p 12
q[7] 8 r[3] 15
w2 28 r[-2] 10
l 24 w 20
x 12 f 32


On Nov 16, 11:48 pm, Addison Denenberg <addison.denenb...@gmail.com>
wrote:
> I think this has something to do with the unsigned and int in the
> declaration along with short. I printed out the size of w when I ran it and
> the size is 4 so now all of those addresses make sense and why w is at 20
> and not 18. So I think maybe the unsigned and int in the declaration
> override the short somehow.
>
> Addison
>
> On Mon, Nov 16, 2009 at 11:26 PM, David L. Rager <rage...@gmail.com> wrote:
>
>
>
> > Ya'll should just run the C code I sent out in this thread.  It's
> > setup to give you the answers to the test question.
>
> >http://groups.google.com/group/utexas-cs352-fall2009/browse_thread/th...
Reply all
Reply to author
Forward
0 new messages