Exam 4 Review, Quiz 4, Convert, Aggregation+composition – Cse100 Programming With C++ 11/09

Star on the on the left side of the put her here the second one can anyone tell me why this one’s wrong yeah yeah add parentheses so would it what is what would this print if I.

Just ran this by itself it would print thirteen y-yeah and numbers is the pointer to the start of the array right here i dereference that so that gives me the value ten yeah oh sorry it should be you the third element would be it’ll.

Print forty is what we want it to print the plus three here is move over three indices so you’re right it would want.

To print forty but what this actually prints is thirteen because star numbers here just looks up the first address in the array and then after it looks up the value right here then it does.

A plus 3 but remember pointer arithmetic we want it to do the addition first than the dereference yeah yeah it should yeah so if if you put like error because I screwed up or something yeah I’ll give you credit for that too yeah yeah of course it has.

To be the same yeah my mistake yeah you’re totally right okay can anyone tell me why this one’s wrong yeah I need the square bracket on delete because I’m.

Deleting an array not a single element so that one’s pretty straightforward what about this one yeah yeah yeah so here I don’t need.

An asterisk because I want to set the address to the.

Pointer itself not the memory location sorry not the value at that place so a star pointer now if I ran this would contain the value would contain the a dress of X not the pointer itself yeah no no you can in here yeah because they’re both intz is just the star.

Is not a different type so they’re both integers but it’s just one as a pointer.

And what it’s not so I would initialize these on different lines.

But you’re allowed to do that unless you ran it and then something happened yeah yeah yeah it’s so I’m just yeah so this code kind of makes no sense because X is on the stack here but it’s just can I actually do this though that’s the yeah yeah so that so that that’s a different meaning but it’s.

It makes more sense because then I’m copying the value of x into the value of where pointers pointing to so that would be a different thing but it would still make sense and what we what it seems to me that this wants to do is have pointer point to X and not necessarily like copy the value of where pointer actually is but you’re right you could do that also any other questions about this one good question I actually I just thought of that yeah.

So let’s come at that out yeah I see what you mean come on let’s run hmm oh yeah because I didn’t initialize X plus let’s set it to zero okay yes so this is just copying the value over into the value of what pointer is pointing to let’s actually see out where pointer is actually pointing to because I don’t actually know oh yeah because we didn’t initialize pointer so it could be pointing.

To not necessarily zero which is no pointer it could be pointing to anything just like how an uninitialized variable.

Can have any value same reasoning here yeah so we if we want it to actually do something useful we have to set it to like new int or some other address or something okay yeah.

So it’s initializing the value of put or not the actual pointer itself what about this one so what is a fix for this one we need to put star on put ER on the second line because we want it to point to a new int on the heat okay so if I looked at this let’s do this.

So this would technically work but we want distance hmm okay so I guess there’s a bug here I will revise the solutions for this I thought you needed to put a star here because new int will give you a pointer to something I guess there’s actually technically nothing wrong.

This so if you put nothing wrong with this I’ll give you full I guess everyone will get full credit on that one then yeah yes so it’s really about the lines themselves not necessarily the.

Consequences that happen after but but yeah I’ll give everyone full credit on this one because technically nothing is wrong with this one I thought you needed to put star there but I realized that it’s initializing to the pointer not to the value all right any questions on the first question ya know the wrong way in in terms.

Of being able to manage memory yourself there’s nothing technically wrong with it but it’s just that you it could cause so many problems later on because as I’ve said.

If you return from a function and you don’t call delete now you have a tank quick pointer there’s nothing wrong with it but it’s just it’s so much harder to work with that if I might as well consider it a wrong way of doing it you’re right yeah this this is a dynamic array because I call new here yeah other questions yeah I’m not even sure.

If it’ll actually work but let’s see oops don’t know why that did that hmm let’s see okay yeah I think.

It’s just that it deleted the first element necessary and not everything else yeah yeah yeah yeah I could so let’s let’s do the second element let’s.

See okay so yeah it didn’t delete the second second through end of the array only the first one yeah putting my words in my own mouth hmm okay so this one’s actually interesting so calling delete on something doesn’t necessarily change the value of where it is just that you can’t necessarily access it anymore but I’m not sure why you can here hmm pointers are strange so I’ll take a look into this yeah but other questions.

About the quiz the first part yeah yeah that could work here yeah yeah because is is technically not doing what it’s supposed to yeah okay so the second question why would you use.

Smart pointer over a raw one because.

We don’t have dangling pointers that was pretty straightforward the differences between a unique and a shared footer is that.

Unique litter can only have one owner of the object so only one thing can point to it whereas a shared coder could have more well that’s the only difference and then.

We’re unique litters preferred to a share putter and the other way note that a shared footer can do everything a unique.

Litter could you could just say okay the and be careful and say that a sure.

Of this thing so it acts like a unique putter but I can emulate a shared.