# Unity Answers to Washu's quiz #1?

This topic is 3315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just came across Washu's C++ quiz, when I skimmed through this GDNet thread, and I just finished answering the questions. Can someone check my answers? (He didn't provide the answers for the first quiz)
Quote:
 Given the following three lines of code, answer these questions.int* p = new int[10]; int* j = p + 11; int* k = p + 10; 1.A) Is the behavior of the second line well defined? 1.B) If the behavior second line is well defined, where does the pointer point to? 1.C) What are some of the legal operations that can be performed on the third pointer?
1.A) Yes 1.B) It points to the location immediately after the int array (The address of 'p' + sizeof(int)*10). What's contained there, on the other hand, could be utter gibberish. 1.C) You can use any pointer manipulation operators on it, like any other pointer. (add, subtract, multiply, divide, etc...)
Quote:
 2) What output should the following line of code product? int a = 10; std::cout<
2) 10 9 9
Quote:
 3) Assuming the function called in the following block of code has no default parameters, how many parameters does it take? Which objects are passed to it? f((a, b, c), d, e, ((g, h), i));
3) It takes four parameters. (a, d, e, and g)
Quote:
 4) Assuming the function called in the following block of code takes an A* and a B*, what is bad about the code?f(new A(), new B());
4) What's bad is that you'll leak memory unless 'f' knows to free both 'A' and 'B', because the scope of A and B will end immediately following the function call, and so the user has no chance to free it themselves. 'f' must do the freeing (or else keep pointers to them to free later, depending on what the function is used for), and 'f' must also do the error checking, in case 'new' fails.
I know I made a few mistakes, from reading this, but did I get any of the other questions right?

##### Share on other sites
Quote:
 1.A) Yes

Actually, I don't think so. You're allowed to point one past the end of the array, but not past that. The last element of the array is (p + 9). So one past is (p + 10). (p + 11) is undefined.

Quote:
 1.C) You can use any pointer manipulation operators on it, like any other pointer. (add, subtract, multiply, divide, etc...)

You can't multiply/divide pointers.

Quote:
 2) 10 9 9

Isn't this undefined behaviour? a, a++ and --a could be evaluated in any order.

Quote:
 4) What's bad is that you'll leak memory unless 'f' knows to free both 'A' and 'B', because the scope of A and B will end immediately following the function call, and so the user has no chance to free it themselves. 'f' must do the freeing (or else keep pointers to them to free later, depending on what the function is used for), and 'f' must also do the error checking, in case 'new' fails.

There's no error checking that can be done. If new fails, an exception is thrown, and memory will leak if either of the allocations succeeded before throwing. That's why this is bad - because you have a 50% chance of leaking memory if either allocation fails.

##### Share on other sites
The answers are here and perhaps in the comments here. Favor the first link. Sadly, you got most of them wrong (or at least partially so) :( But most people do, so its okay.

##### Share on other sites
Quote:
Original post by Sc4Freak
Quote:
 2) 10 9 9

Isn't this undefined behaviour? a, a++ and --a could be evaluated in any order.

Correct -- there are no sequence points seperating the evaluation of a, a++, and --a. Since a is int, this violates the rule of not modifying integral types multiple times between sequence points, the penalty is UB.

Were "a", say, a non-pointer iterator type instead, you'd merely get an unspecified order of evaluation.

Operator precedence is (almost) entirely unrelated to evaluation order -- it just specifies that a+b+c is evaluated as operator+( operator+(a,b), c ) rather than operator+( a , operator+(b,c) ). Only a few operators define any extra sequence points: the intrinsic ",", "&&", "||", and "?:" operators. These only apply to the intrinsic version -- overloaded versions need not apply.

To consider a( b(), c() );, the only (relevant) sequence point is that a() will be executed sometime after both b() and c() have.

Quote:
 From the C++ Standard (ISO/IEC 14882:2003), page 65, with various styling added5 Expressions [expr]...4 Except where noted, the order of evaluation of operands of individual operators and subexpressions of indi-vidual expressions, and the order in which side effects take place, is unspecified.53) Between the previousand next sequence point a scalar object shall have its stored value modified at most once by the evaluationof an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a fullexpression; otherwise the behavior is undefined. [Example: i = v[i++]; // the behavior is unspecified i = 7, i++, i++; // i becomes 9 [MM: intrinsic operator, adds sequence points] i = ++i + 1; // the behavior is unspecified i = i + 1; // the value of i is incremented—end example]

[Edited by - MaulingMonkey on January 23, 2009 7:25:37 PM]

##### Share on other sites
Disclaimer: My answers might be wrong.

Quote:
 Given the following three lines of code, answer these questions.int* p = new int[10];int* j = p + 11;int* k = p + 10;1.A) Is the behavior of the second line well defined?1.B) If the behavior second line is well defined, where does the pointer point to?1.C) What are some of the legal operations that can be performed on the third pointer?

1.A) No
1.B) It is not well defined, it is out of the bounds of the deferenceable range + 1.
1.C) All pointer operations sans dereferencing.

Quote:
 2) What output should the following line of code product?int a = 10;std::cout<

2) Three numbers, values consisting of 10 and 9. Specifics are unspecified since order of evaluation is unspecified.

Edit: Well apparently this is undefined as opposed to unspecified.

Quote:
 3) Assuming the function called in the following block of code has no default parameters, how many parameters does it take? Which objects are passed to it?f((a, b, c), d, e, ((g, h), i));

3) It takes four parameters: c, d, e, i

Quote:
 4) Assuming the function called in the following block of code takes an A* and a B*, what is bad about the code?f(new A(), new B());

4) If one allocation fails then an exception is thrown, if the the other allocation had already succeeded then memory is leaked.

##### Share on other sites
Quote:
Original post by dmatter
Quote:
 2) What output should the following line of code product?int a = 10;std::cout<

2) Three numbers, values consisting of 10 and 9. Specifics are unspecified since order of evaluation is unspecified.

Were it not UB, it could be 11 as well. Consider the case where a++, then a, then --a are evaluated.

Quote:
Quote:
 3) Assuming the function called in the following block of code has no default parameters, how many parameters does it take? Which objects are passed to it?f((a, b, c), d, e, ((g, h), i));

3) It takes four parameters: c, d, e, i

Good catch.

##### Share on other sites

I couldn't remember whether the comma operator evaluates both objects, and returns the first, or evaluates both, and returns the second. Apparently it's the second. (Question 3) The others I just flat out got wrong.

However, I must question Washu's answer to #2.

Quote:
 2. This line of code has two problems. The first is that the order in which function parameters are evaluated is unspecified. As such, a, a++, and --a can all be processed in any order. However, there is a bigger problem. We are not allowed to modify the same object more than once between sequence points, yet in this case, we have done so twice. As such the behavior of this code is undefined. It could legally obliterate the universe.

std::cout isn't really a function. It's overloading the << operator, and the << operator has a defined order of which objects are evaluated first.

It'd evaluate to:
(((std::cout << a) << a++) << --a);

With each pair of parapheses returning a reference to std::out. (Like here)

It'd print 10, 10, 10 every time, regardless of compiler. (I said 10 9 9, because like the comma operator, I mistakenly swapped the order of evaluation in my mind)

Isn't that correct?

[Edit:] Others posted before I finished my post, so my response as already been broken before it even became public. [smile]

Isn't C++ fun?

##### Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
 Original post by dmatter2) Three numbers, values consisting of 10 and 9. Specifics are unspecified since order of evaluation is unspecified.

Were it not UB, it could be 11 as well. Consider the case where a++, then a, then --a are evaluated.
Oops, thanks for the catch, 11 slipped by me.

##### Share on other sites
Quote:
 Original post by Servant of the LordIt'd print 10, 10, 10 every time, regardless of compiler. (I said 10 9 9, because like the comma operator, I mistakenly swapped the order of evaluation in my mind)

I'm bored.

MSVC 9, Release: 9910
MSVC 9, Debug: 10910
MSVC 8, Release: 9910
MSVC 8, Debug: 10910
MSVC 7.1, Release: 9910
MSVC 7.1, Debug: 10910
g++ 3.4.4 cygming -O0: 101010
g++ 3.4.4 cygming -O1: 101010
g++ 3.4.4 cygming -O2: 101010
g++ 3.4.4 cygming -O3: 101010
g++ 3.4.4 cygming -Os: 101010
Borland Turbo C++ 2006, Debug: 1099
Borland Turbo C++ 2006, -O1: 1099
Borland Turbo C++ 2006, -O2: 1099

##### Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by Servant of the LordIt'd print 10, 10, 10 every time, regardless of compiler. (I said 10 9 9, because like the comma operator, I mistakenly swapped the order of evaluation in my mind)

I'm bored.

MSVC 9, Release: 9910
MSVC 9, Debug: 10910
MSVC 8, Release: 9910
MSVC 8, Debug: 10910
MSVC 7.1, Release: 9910
MSVC 7.1, Debug: 10910
g++ 3.4.4 cygming -O0: 101010
g++ 3.4.4 cygming -O1: 101010
g++ 3.4.4 cygming -O2: 101010
g++ 3.4.4 cygming -O3: 101010
g++ 3.4.4 cygming -Os: 101010
Borland Turbo C++ 2006, Debug: 1099
Borland Turbo C++ 2006, -O1: 1099
Borland Turbo C++ 2006, -O2: 1099

Good lord, you have all of those installed? Whatever for?

##### Share on other sites
Quote:
Original post by Servant of the Lord

I couldn't remember whether the comma operator evaluates both objects, and returns the first, or evaluates both, and returns the second. Apparently it's the second. (Question 3) The others I just flat out got wrong.

However, I must question Washu's answer to #2.

Quote:
 2. This line of code has two problems. The first is that the order in which function parameters are evaluated is unspecified. As such, a, a++, and --a can all be processed in any order. However, there is a bigger problem. We are not allowed to modify the same object more than once between sequence points, yet in this case, we have done so twice. As such the behavior of this code is undefined. It could legally obliterate the universe.

std::cout isn't really a function. It's overloading the << operator, and the << operator has a defined order of which objects are evaluated first.

It'd evaluate to:
(((std::cout << a) << a++) << --a);

With each pair of parapheses returning a reference to std::out. (Like here)

It'd print 10, 10, 10 every time, regardless of compiler. (I said 10 9 9, because like the comma operator, I mistakenly swapped the order of evaluation in my mind)

Isn't that correct?

[Edit:] Others posted before I finished my post, so my response as already been broken before it even became public. [smile]

int a = 10; std::cout.operator <<(a).operator<<(a++).operator<<(--a);

The order of evaluation of function parameters is unspecified except in that they will be evaluated before the function is called. Furthermore, the behavior is UNDEFINED because that evaluation has no sequence points, thus allowing a to be modified more than once between sequence points.

##### Share on other sites
Quote:
 Original post by Mike.PopoloskiGood lord, you have all of those installed? Whatever for?

MSVC 9 is installed because it's the latest and greatest MSVC edition. MSVC 8 is there mostly so I can say "No, the code works just fine under MSVC 2005. You're doing something else wrong and it's not the compiler's fault". MSVC 7.1 is installed because I still create single threaded applications. g++ is installed because it's often good to run your code through a different compiler to see if it catches things your primary compiler doesn't. And Borland Turbo C++ is installed because I still have a couple of programs that use VCL that I use.

##### Share on other sites
Quote:
 Given the following three lines of code, answer these questions.int* p = new int[10];int* j = p + 11;int* k = p + 10;

I'm curious about this one, espeically the undefined behaviour of j. Is it the referencing of j that would be undefined, or is it that the range of integer operations on j is only defined for the interval [p, p + 10)? If the next line was something like j = j - 10, would or would not j still produce undefined behaviour?

##### Share on other sites
Quote:
Original post by Trapper Zoid
Quote:
 Given the following three lines of code, answer these questions.int* p = new int[10];int* j = p + 11;int* k = p + 10;

I'm curious about this one, espeically the undefined behaviour of j. Is it the referencing of j that would be undefined, or is it that the range of integer operations on j is only defined for the interval [p, p + 10)? If the next line was something like j = j - 10, would or would not j still produce undefined behaviour?

p+11 //undefined.

##### Share on other sites
Quote:
 Original post by Washup+11 //undefined.

So to clarify: j starts as who-knows-what and will remain who-knows-what under operations until reassigned?

##### Share on other sites
Quote:
Original post by Trapper Zoid
Quote:
 Original post by Washup+11 //undefined.

So to clarify: j starts as who-knows-what and will remain who-knows-what under operations until reassigned?

no. At the point p+11 is performed, the behavior of the application is completely undefined. The universe could be destroyed by that program and it would be completely OK. Because p+11 is undefined behavior.

##### Share on other sites
Quote:
 Original post by Washuno. At the point p+11 is performed, the behavior of the application is completely undefined. The universe could be destroyed by that program and it would be completely OK. Because p+11 is undefined behavior.

Ah, I get it now.

So, is "destruction of the universe" error code #1? And, uh, what CPU chipset do I need to compile for to get that kind of undefined behaviour (and where can I get one?) *whistles innocently*

##### Share on other sites
Quote:
Original post by Trapper Zoid
Quote:
 Original post by Washuno. At the point p+11 is performed, the behavior of the application is completely undefined. The universe could be destroyed by that program and it would be completely OK. Because p+11 is undefined behavior.

Ah, I get it now.

So, is "destruction of the universe" error code #1? And, uh, what CPU chipset do I need to compile for to get that kind of undefined behaviour (and where can I get one?) *whistles innocently*

Well, you can't. My system is the only one in existance, and it's currently busy running the universe (written in python, of course). More to the point, I've used certain types of chips that used specialized pointer registers and would validate them as well. On such a system, executing p+11 could result in the system killing the process with an error.

##### Share on other sites
The starter kit (for those unlucky enough to not be washu) is one nuclear ICBM with just about any chipset. Load up some C++ code and you're good to go! Just add undefined behavior and BOOM! Wrong target / detonation time / arming status. Best served with a side of guacamole.

##### Share on other sites
Quote:
 Original post by WashuWell, you can't. My system is the only one in existance, and it's currently busy running the universe (written in python, of course). More to the point, I've used certain types of chips that used specialized pointer registers and would validate them as well. On such a system, executing p+11 could result in the system killing the process with an error.

Python avoids pointers, doesn't it? Don't tell me you're running poorly tested extension libraries on that thing!

Oh, and I have some quantum physicists here who want to tell you that your system's floating point unit sucks.

##### Share on other sites
I think the universe would use a fixed point format. The planck length doesn't really vary with your position. Furthermore, the universe's fixed point format used for storing position seems to use a 116-bit mantissa!

##### Share on other sites
Quote:
 Original post by MaulingMonkeyIsn't C++ fun?

Yes. The only reason I own so many books about C++ is because I'm partial for languages with many corner cases :)

##### Share on other sites
Quote:
Original post by Trapper Zoid
Quote:
 Original post by Washuno. At the point p+11 is performed, the behavior of the application is completely undefined. The universe could be destroyed by that program and it would be completely OK. Because p+11 is undefined behavior.

Ah, I get it now.

So, is "destruction of the universe" error code #1? And, uh, what CPU chipset do I need to compile for to get that kind of undefined behaviour (and where can I get one?) *whistles innocently*

The destruction of the universe will not be heralded by an error code, unfortunately. It will be a run-time error.

__

In other news, I got the answers to the first quiz right! Awesome. I'm reading the C++ Programming Language volume 3 right now though and it is very explicit about what is and is not defined. Most C++ programming books are not so detailed.

##### Share on other sites
Quote:
 Original post by Washup+11 //undefined.`

In particular, p + 11 could result in integer overflow. This could result in an integer overflow exception, which in turn could crash the program. It could also result in wrapping so that p + 11 would be less than p + 10 (and less than p for that matter). It could also result in saturation so that (p + 11) - 10 could be less than p + 1 and greater than p, and probably unaligned.

• 9
• 50
• 11
• 17
• 11
• ### Similar Content

• By NfiniT
Hello!  Our team is a searching for a 2D Artist looking to add a title (whether the first one, or one of many) to his portfolio, with the opportunity to pursue a long-term partnership.

Position & Responsibilities:
Our team is looking for a 2D Artist to help produce finalized art assets from our current concepts and help finish our current working prototype.  This includes various stylized cartoon characters, level components/objects, background art, and possible UI elements.  The entire team can either provide you the concept art, or let you have extended creative freedom, it's up to you--whichever you feel is better suited for your process.
Also, although this is not a requirement, if you want to expand your skill-base, you can dabble in the other aspects as well, be it animation, programming, design, narrative, etc.
Project Description:
Our project is titled Stanley and the Clocktower Caper, and is a casual 2D platformer with a puzzle element and child-friendly combat.  Our goal is a funny, relaxing experience that intentionally does not take itself seriously.
Set in the steam-punk styled town, our protagonist Stanley (a sloth), is a local tinkerer and keeper of the town's clocktower.  The town and clocktower have been invaded (story-related reasoning) and are swarming with spiders who've gummed up the cogs with webs, removed gears and all-around caused chaos.  Stanley must make his way down, out of the clocktower, which will require unclogging gears, finding missing pieces to levers, switches, gear-trains, etc.  While all of this is a tall order, thankfully Stanley is an avid consumer of Slothbux, a particularly caffeinated brand of coffee which gives Stanley quite the pick-me-up (how else do you think a sloth manages to be a tinkerer and get so many odd things done!? ) and allows him to accomplish some feats of speed, agility and strength not normally attainable otherwise!
This game will consist of Stanley escaping the tower, which is apparently going to be a pretty big tower for such a little sloth!  It must have a lot of clocks...  Pending good reception on release (this is discussed below), we would happily consider a follow-up adventure.
We'll be releasing on PC, and are targeting a July release--it is an admittedly small window, but a lot of the functionality already exists and we've got a realistic perception of what we're capable of as a small team and have kept the scope of our project focused accordingly.  Distribution channel is not finalized, though the most obvious and likely candidates are Steam and Itch.io.  Price-point of the final product is TBD.
You will be included in the rev-share, and of course that's regardless of whether you fill the role and leave or decide to stay with us for future titles.
Team:
There are currently four of us, you will be the fifth.
1) (Me) Our resident programmer, and dabble in both concept art & Design.
2) Our producer (handles scheduling, keeping things organized and on task), who also handles animation & dabbles in design.
3) Our lead designer (story, level layout, etc.), who also does a bit of concept art
4) Our sound engineer, who is also using the opportunity to learn a bit of programming.
Working prototype--https://iconoclast209.itch.io/stanley

If you would like to fill the role or have more questions, feel free to reach out to me on PM or on Discord at Bryant - NfiniT#5890

• Aloha,
First time working in 3D. Not as difficult as I thought it would be although I am pretty much a complete beginner in Blender. I chose the low poly style because I like how it looks and it is a lot easier than trying to make it look realistic.
The gameplay is pretty much all physics driven which makes it feel satisfying to play. If it looks any fun then please check it out. I could really use a review or two.
Here are some promo codes so that you don't have to pay a dime to download it:
PJW7MTMWL3W3
A4JLYRPTXMJH
AXRKPMAT7AAY
YMFJPTPJ9EX3
JY3W6FNKTM6L

• I'm working on a space game, and I suck at art. I would love to get some help from someone who is more skilled than me. Things I need include modular space ship parts and GUI elements. Nothing too fancy, just functional so I can get a prototype put together. This could potentially become a serious project, but for now this is just a hobby project.

In this video, you can see a few things I already completed
:2018-02-24 20-08-13.flv2018-02-24 20-08-13.flv

• If you want to incorporate noise into your shaders, the Turbulance Library has you covered. Using code I gathered from this library, I made a cginc file that contains all you need to easily implement noise into your unity shaders. Who knows how this stuff works, but man, does it work well!

https://pastebin.com/LLCUpJut

Here is an example of what you can create using these noise functions.

• I'll be buying a new laptop as my workstation for building games, Mostly 3D but not hard core.

I'm stuck at choosing between these 2 specs below. Does this really matter and if so, can some one tell my how and why it matters.
Choice1:
Intel core i5-8250U (8th gen Kabylake refresh)(6 MB Smart Cache, 1.6 GHz Base with Turbo Boost up to 3.4 GHz) 4 cores 8 threads
RAM 8 GB DDR4 (2400 MHz)
GPU 2 GB DDR5 Nvidia MX150 256 bit
SSD: yes
Choice2:
Intel core i7-7500U 2.70GHz Base Processor (4M Cache, up to 3.50 GHz Boost) 2 Cores, 4 Threads
RAM 4 GB DDR4 (1800 MHz)
GPU 2 GB DDR5 Nvidia GeForce 940MX 256 bit
SSD: No