Week 2 is up

183 views
Skip to first unread message

Arrvindh Shriraman

unread,
Sep 11, 2020, 12:52:52 PM9/11/20
to cmpt-295-sfu
- Respond to this message with any questions you want discussed next week
- Please use the lecture question template included in the welcome message
- Struct_MultiD.pptx will be synchronously taught in class next week.

Hyeonyoung Park

unread,
Sep 15, 2020, 3:53:36 PM9/15/20
to cmpt-295-sfu
# Lecture Question
Week : 2 
Slide deck name: Week 2 Memory lll - Heap (L02 Slide # 20), 2nd video at 7:38
Question: 

In Week 1 L03 slide #22, we learned that p[1] = *(p+1). I thought by adding 1, it will be scaled by the size of the type using the address arithmetic. So if the type is int, it would multiply 1 with 4.

However, on week 2 L02 slide #20, we learned that p[1] = *(p+4) when the pointer type is int. I got confused because I thought the compiler is the one who scale the size of the type not us. Am I mistaken? Please educate me if I am wrong. Thank you for your time.

2020년 9월 11일 금요일 오전 9시 52분 52초 UTC-7에 ashrir...@gmail.com님이 작성:

Arrvindh Shriraman

unread,
Sep 15, 2020, 4:09:47 PM9/15/20
to cmpt-2...@googlegroups.com


On Tuesday, September 15, 2020 at 12:53:36 PM UTC-7, Hyeonyoung Park wrote:
# Lecture Question
Week : 2 
Slide deck name: Week 2 Memory lll - Heap (L02 Slide # 20), 2nd video at 7:38
Question: 

In Week 1 L03 slide #22, we learned that p[1] = *(p+1). I thought by adding 1, it will be scaled by the size of the type using the address arithmetic. So if the type is int, it would multiply 1 with 4.


This is correct.
 
However, on week 2 L02 slide #20, we learned that p[1] = *(p+4) when the pointer type is int. I got confused because I thought the compiler is the one who scale the size of the type not us. Am I mistaken? Please educate me if I am wrong. Thank you for your time.

L02 ? Week 2 does not have L02 slide deck. 

If if helps, the following are equivalent
p[1] 
*(p+1) - Compiler takes care off figuring out size of type. The RHS of + is scaled by the sizeof(T).
(void*)p + sizeof(T) - Programmer has to figure out  size of type. Note the case of p type to void*. So now any value to RHS of + has to be in bytes. 

-----------------------------------------------------------------
Is this the video https://youtu.be/TkU1sxJI4EM (min: 7:12) you are talking about? 


If so, I realized what I said may come of as saying 
p[1] = *(p+4). 
But that was not my intention. 

I was thinking in terms of value.
The address of p[1] is the address + integer value 4.  
e.g., 
p = 0x80. I am thinking p[1] = *(0x80 + 4)

Thanks; I have added some clarification in the description now.
Message has been deleted

Hyeonyoung Park

unread,
Sep 15, 2020, 4:22:35 PM9/15/20
to cmpt-295-sfu
Oh, sorry for posting the wrong number slide. Yes, that was what I was confused wit. Thank you so much for your explanation. That makes so much more sense. Have a great day!

2020년 9월 15일 화요일 오후 1시 9분 47초 UTC-7에 ashrir...@gmail.com님이 작성:

Hyeonyoung Park

unread,
Sep 15, 2020, 4:46:31 PM9/15/20
to cmpt-295-sfu
Hello, I actually have another question about regarding my question. So does this mean the pointer arithmetic comes in to place only when you use the variable? So if we use the actual value when adding values to address, would we not think of it as a pointer arithmetic? 
2020년 9월 15일 화요일 오후 1시 9분 47초 UTC-7에 ashrir...@gmail.com님이 작성:

Arrvindh Shriraman

unread,
Sep 17, 2020, 11:41:56 AM9/17/20
to cmpt-295-sfu
Reminder to preload this thread with questions you may have. 
We do not want waste class time on me sifting through 10 point font in a chat box :) (if anyone knows how to increase font size within zoom, I am all ears).

Arrvindh Shriraman

unread,
Sep 20, 2020, 8:34:06 PM9/20/20
to cmpt-295-sfu
Sep 18 classroom lecture and remaining slides on Struct-MultiD have now been posted.
Reply all
Reply to author
Forward
0 new messages