Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #88013 > unrolled thread
| Started by | Roman P <invalid@example.com> |
|---|---|
| First post | 2022-12-18 20:00 +0000 |
| Last post | 2022-12-21 12:02 +0100 |
| Articles | 19 — 12 participants |
Back to article view | Back to comp.lang.c++
Christmas Quiz 2022 Roman P <invalid@example.com> - 2022-12-18 20:00 +0000
Re: Christmas Quiz 2022 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-18 21:22 +0000
Re: Christmas Quiz 2022 red floyd <no.spam.here@its.invalid> - 2022-12-18 13:36 -0800
Re: Christmas Quiz 2022 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-18 13:48 -0800
Re: Christmas Quiz 2022 Siri Cruise <chine.bleu@yahoo.com> - 2022-12-18 14:57 -0800
Re: Christmas Quiz 2022 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-18 15:43 -0800
Re: Christmas Quiz 2022 Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-19 23:40 -0600
Re: Christmas Quiz 2022 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-20 14:01 -0800
Re: Christmas Quiz 2022 "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-12-18 15:18 -0800
Re: Christmas Quiz 2022 Kaz Kylheku <864-117-4973@kylheku.com> - 2022-12-20 13:49 +0000
Re: Christmas Quiz 2022 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-20 14:33 +0000
Re: Christmas Quiz 2022 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-12-20 14:44 +0000
Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 12:46 -0800
Re: Christmas Quiz 2022 Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-20 23:49 +0200
Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 14:00 -0800
Re: Christmas Quiz 2022 Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-21 00:13 +0200
Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 14:38 -0800
Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 21:31 -0800
Re: Christmas Quiz 2022 David Brown <david.brown@hesbynett.no> - 2022-12-21 12:02 +0100
| From | Roman P <invalid@example.com> |
|---|---|
| Date | 2022-12-18 20:00 +0000 |
| Subject | Christmas Quiz 2022 |
| Message-ID | <tnnspa$27vkp$1@paganini.bofh.team> |
1) What happens when you divide an int variable by 0? 2) What happens when you divide a double variable by 0? 3) What happens when you overflow an int variable, that is, set it to a value beyond its range? 4) What is the difference between x = y++; and x = ++y;? 5) What is the difference between break, continue, and return when used inside a loop statement? 6) What are the three parts of a for statement and which of them are required? 7) What is the difference between the = and == operators? 8) Does the following statement compile? for ( ; ; ) ; 9) What does the underscore _ represent in a switch expression? Merry Christmas to you all.
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2022-12-18 21:22 +0000 |
| Message-ID | <87zgbkxv8n.fsf@bsb.me.uk> |
| In reply to | #88013 |
Roman P <invalid@example.com> writes: I now see that this is cross-posted to comp.lang.c++ as well as to comp.lang.c. That's not a good idea for quizzes like this. > 1) What happens when you divide an int variable by 0? Asking what "happens" in this sort of context is a bit odd, but maybe clarifying what this might mean is part of the question? > 2) What happens when you divide a double variable by 0? > 3) What happens when you overflow an int variable, that is, set it to a > value beyond its range? And here there is even more ambiguity since a variable (what might better be called a named object) can be set in two ways, and the C standard says different things about each! > 4) What is the difference between x = y++; and x = ++y;? > 5) What is the difference between break, continue, and return when used > inside a loop statement? > 6) What are the three parts of a for statement and which of them are > required? The correct answer for C was made more complicated in 1999, and as for C++, well that's a whole new ball game. > 7) What is the difference between the = and == operators? > 8) Does the following statement compile? > > for ( ; ; ) ; > > 9) What does the underscore _ represent in a switch expression? There is no switch expression in C and a switch statement can include lots of expressions. But, that aside, I'd like to see what you think the answer is. > Merry Christmas to you all. And to you too. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | red floyd <no.spam.here@its.invalid> |
|---|---|
| Date | 2022-12-18 13:36 -0800 |
| Message-ID | <tno152$21h3$1@redfloyd.dont-email.me> |
| In reply to | #88013 |
On 12/18/2022 12:00 PM, Roman P wrote: > 1) What happens when you divide an int variable by 0? > 2) What happens when you divide a double variable by 0? > 3) What happens when you overflow an int variable, that is, set it to a > value beyond its range? > 4) What is the difference between x = y++; and x = ++y;? > 5) What is the difference between break, continue, and return when used > inside a loop statement? > 6) What are the three parts of a for statement and which of them are > required? > 7) What is the difference between the = and == operators? > 8) Does the following statement compile? > > for ( ; ; ) ; > > 9) What does the underscore _ represent in a switch expression? > Do your own homework.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2022-12-18 13:48 -0800 |
| Message-ID | <tno1qh$21o4$2@dont-email.me> |
| In reply to | #88013 |
On 12/18/2022 12:00 PM, Roman P wrote: > 1) What happens when you divide an int variable by 0? [...] Undefined behavior.
[toc] | [prev] | [next] | [standalone]
| From | Siri Cruise <chine.bleu@yahoo.com> |
|---|---|
| Date | 2022-12-18 14:57 -0800 |
| Message-ID | <chine.bleu-93D7AF.14570318122022@news.eternal-september.org> |
| In reply to | #88017 |
In article <tno1qh$21o4$2@dont-email.me>, "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote: > On 12/18/2022 12:00 PM, Roman P wrote: > > 1) What happens when you divide an int variable by 0? > [...] > > Undefined behavior. https://www.youtube.com/watch?v=JOiZP8FS5Ww Saved by zero. -- :-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @ 'I desire mercy, not sacrifice.' /|\ Discordia: not just a religion but also a parody. This post / \ I am an Andrea Chen sockpuppet. insults Islam. Mohammed
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2022-12-18 15:43 -0800 |
| Message-ID | <tno8jq$2mfh$2@dont-email.me> |
| In reply to | #88018 |
On 12/18/2022 2:57 PM, Siri Cruise wrote: > In article <tno1qh$21o4$2@dont-email.me>, > "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote: > >> On 12/18/2022 12:00 PM, Roman P wrote: >>> 1) What happens when you divide an int variable by 0? >> [...] >> >> Undefined behavior. > > https://www.youtube.com/watch?v=JOiZP8FS5Ww > > Saved by zero. > No shit! lol. :^) I have worked on some stuff that would detect a divide by zero condition and altered it before it occurred. It would log the condition, then change the denominator.
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-19 23:40 -0600 |
| Message-ID | <tnrhsv$k34f$1@dont-email.me> |
| In reply to | #88021 |
On 12/18/2022 5:43 PM, Chris M. Thomasson wrote: > On 12/18/2022 2:57 PM, Siri Cruise wrote: >> In article <tno1qh$21o4$2@dont-email.me>, >> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote: >> >>> On 12/18/2022 12:00 PM, Roman P wrote: >>>> 1) What happens when you divide an int variable by 0? >>> [...] >>> >>> Undefined behavior. >> >> https://www.youtube.com/watch?v=JOiZP8FS5Ww >> >> Saved by zero. >> > > No s***! lol. :^) > > I have worked on some stuff that would detect a divide by zero condition > and altered it before it occurred. It would log the condition, then > change the denominator. We set the result of all divisions by zero to be zero. Not perfect but usually is a initialization problem. Lynn
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2022-12-20 14:01 -0800 |
| Message-ID | <tntbcb$q15e$1@dont-email.me> |
| In reply to | #88018 |
On 12/18/2022 2:57 PM, Siri Cruise wrote: > In article <tno1qh$21o4$2@dont-email.me>, > "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote: > >> On 12/18/2022 12:00 PM, Roman P wrote: >>> 1) What happens when you divide an int variable by 0? >> [...] >> >> Undefined behavior. > > https://www.youtube.com/watch?v=JOiZP8FS5Ww > > Saved by zero. > the null lines: https://youtu.be/MC4yCPbdbT4 (lord of the null lines)
[toc] | [prev] | [next] | [standalone]
| From | "daniel...@gmail.com" <danielaparker@gmail.com> |
|---|---|
| Date | 2022-12-18 15:18 -0800 |
| Message-ID | <9deff0eb-ffa6-4742-86c0-7fa85d2e54c8n@googlegroups.com> |
| In reply to | #88013 |
On Sunday, December 18, 2022 at 3:22:18 PM UTC-5, Roman P wrote: > 1) What happens Also of interest, what happens when you write code that calls substr on a std::basic_string<char,std::char_traits<char>,MyAllocator> when the template parameter MyAllocator is a stateful allocator?
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <864-117-4973@kylheku.com> |
|---|---|
| Date | 2022-12-20 13:49 +0000 |
| Message-ID | <20221220054654.91@kylheku.com> |
| In reply to | #88013 |
On 2022-12-18, Roman P <invalid@example.com> wrote: > 6) What are the three parts of a for statement and which of them are > required? Check your fingers; I count nine: for ( init ; test ; step ) stmt 1 2 3 4 5 6 7 8 9 -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2022-12-20 14:33 +0000 |
| Message-ID | <87tu1quou7.fsf@bsb.me.uk> |
| In reply to | #88129 |
Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2022-12-18, Roman P <invalid@example.com> wrote:
>> 6) What are the three parts of a for statement and which of them are
>> required?
>
> Check your fingers; I count nine:
>
> for ( init ; test ; step ) stmt
> 1 2 3 4 5 6 7 8 9
or possibly 8:
for ( declaration test ; step ) stmt
1 2 3 4 5 6 7 8
depending on what you include in "init".
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2022-12-20 14:44 +0000 |
| Message-ID | <tnshp9$mh7i$1@dont-email.me> |
| In reply to | #88133 |
On Tue, 20 Dec 2022 14:33:36 +0000, Ben Bacarisse wrote: > Kaz Kylheku <864-117-4973@kylheku.com> writes: > >> On 2022-12-18, Roman P <invalid@example.com> wrote: >>> 6) What are the three parts of a for statement and which of them are >>> required? >> >> Check your fingers; I count nine: >> >> for ( init ; test ; step ) stmt 1 2 3 4 5 6 7 8 9 > > or possibly 8: > > for ( declaration test ; step ) stmt 1 2 3 4 5 6 7 > 8 > > depending on what you include in "init". The C11 draft standard defines the for statement as for ( clause-1 ; expression-2 ; expression-3 ) statement where clause-1 may be a declaration or an expression. The notable point is that the semicolon between clause-1 and expression-2 is /not/ part of clause-1, but is, instead part of the for statement itself. Because of this, I don't believe that your count can be correct. But, I'm willing to be educated on this :-) -- Lew Pitcher "In Skills, We Trust"
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2022-12-20 12:46 -0800 |
| Message-ID | <tnt6vc$pgk6$1@dont-email.me> |
| In reply to | #88013 |
On 12/18/2022 12:00 PM, Roman P wrote: > 1) What happens when you divide an int variable by 0? > 2) What happens when you divide a double variable by 0? Undefined behavior. Nevertheless: what does "divide" mean in this context? There's more than one way to perform arithmetic division in C and C++. > 3) What happens when you overflow an int variable, that is, set it to a > value beyond its range? "Set"? What does "set" mean in this context? Behavior of variables in out-of-range situations depends critically on _how_ you attempt to produce an out-of-range value in it. For example, assignment is one thing, while side effect of `++` is a completely different thing. > 4) What is the difference between x = y++; and x = ++y;? Quite possibly, none. There's no reason to believe this code has any observable behavior. > 6) What are the three parts of a for statement and which of them are > required? Even under superficial observation, for-statement has way more than three parts. > 8) Does the following statement compile? > > for ( ; ; ) ; In C++: no way to say. An infinite loop that has no observable behavior produces undefined behavior. Possible manifestations of undefined behavior include failure to compile. > 9) What does the underscore _ represent in a switch expression? Nonsensical question. There's no such thing as "switch expression" neither in C nor in C++. -- Best regards, Andrey
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2022-12-20 23:49 +0200 |
| Message-ID | <tntalf$ptr7$1@dont-email.me> |
| In reply to | #88159 |
20.12.2022 22:46 Andrey Tarasevich kirjutas: >> 4) What is the difference between x = y++; and x = ++y;? > > Quite possibly, none. There's no reason to believe this code has any > observable behavior. I guess he got you there. The value of x is observable. Too well-defined question?
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2022-12-20 14:00 -0800 |
| Message-ID | <tntb9j$q06h$1@dont-email.me> |
| In reply to | #88160 |
On 12/20/2022 1:49 PM, Paavo Helde wrote: > 20.12.2022 22:46 Andrey Tarasevich kirjutas: > >>> 4) What is the difference between x = y++; and x = ++y;? >> >> Quite possibly, none. There's no reason to believe this code has any >> observable behavior. > > I guess he got you there. The value of x is observable. Too well-defined > question? I don't know what you are trying to say by "the value of x is observable", but let me reiterate: there's no reason to believe this code has any observable behavior. (Feel free to look up the definition.) Let alone the fact that in C++ it has no specific semantics at all. -- Best regards, Andrey
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2022-12-21 00:13 +0200 |
| Message-ID | <tntc2r$ptr7$2@dont-email.me> |
| In reply to | #88161 |
21.12.2022 00:00 Andrey Tarasevich kirjutas:
> On 12/20/2022 1:49 PM, Paavo Helde wrote:
>> 20.12.2022 22:46 Andrey Tarasevich kirjutas:
>>
>>>> 4) What is the difference between x = y++; and x = ++y;?
>>>
>>> Quite possibly, none. There's no reason to believe this code has any
>>> observable behavior.
>>
>> I guess he got you there. The value of x is observable. Too
>> well-defined question?
>
> I don't know what you are trying to say by "the value of x is
> observable", but let me reiterate: there's no reason to believe this
> code has any observable behavior. (Feel free to look up the definition.)
> Let alone the fact that in C++ it has no specific semantics at all.
The questions were cross-posted to C so I'm assuming int.
#include <iostream>
void f1() {
int y = 0;
int x = y++;
std::cout << "x = " << x << "\n";
}
void f2() {
int y = 0;
int x = ++y;
std::cout << "x = " << x << "\n";
}
int main() {
f1();
f2();
}
Output:
x = 0
x = 1
Different enough?
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2022-12-20 14:38 -0800 |
| Message-ID | <tntdgj$q76c$1@dont-email.me> |
| In reply to | #88163 |
On 12/20/2022 2:13 PM, Paavo Helde wrote:
> 21.12.2022 00:00 Andrey Tarasevich kirjutas:
>> On 12/20/2022 1:49 PM, Paavo Helde wrote:
>>> 20.12.2022 22:46 Andrey Tarasevich kirjutas:
>>>
>>>>> 4) What is the difference between x = y++; and x = ++y;?
>>>>
>>>> Quite possibly, none. There's no reason to believe this code has any
>>>> observable behavior.
>>>
>>> I guess he got you there. The value of x is observable. Too
>>> well-defined question?
>>
>> I don't know what you are trying to say by "the value of x is
>> observable", but let me reiterate: there's no reason to believe this
>> code has any observable behavior. (Feel free to look up the
>> definition.) Let alone the fact that in C++ it has no specific
>> semantics at all.
>
> The questions were cross-posted to C so I'm assuming int.
>
> #include <iostream>
>
> void f1() {
> int y = 0;
> int x = y++;
> std::cout << "x = " << x << "\n";
> }
>
> void f2() {
> int y = 0;
> int x = ++y;
> std::cout << "x = " << x << "\n";
> }
>
> int main() {
> f1();
> f2();
> }
>
> Output:
>
> x = 0
> x = 1
>
> Different enough?
Oh, yes. Quite different. But this is really an answer to a different
question. Say
What is the difference between
int y = 0;
int x = y++;
std::cout << "x = " << x << "\n";
and
int y = 0;
int x = ++y;
std::cout << "x = " << x << "\n";
?
--
Best regards,
Andrey
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2022-12-20 21:31 -0800 |
| Message-ID | <tnu5oc$v3nu$1@dont-email.me> |
| In reply to | #88159 |
On 12/20/2022 12:46 PM, Andrey Tarasevich wrote: > On 12/18/2022 12:00 PM, Roman P wrote: > >> 3) What happens when you overflow an int variable, that is, set it to a >> value beyond its range? > > "Set"? What does "set" mean in this context? > > Behavior of variables in out-of-range situations depends critically on > _how_ you attempt to produce an out-of-range value in it. For example, > assignment is one thing, while side effect of `++` is a completely > different thing. > ... although here I'm probably trying to be too smart for my own good: there's no need for this kind of branching. In the end, side effects modify the variable value through assignment as well. This means that trying to "set an int to a value beyond its range" results in a congruent value in C++ and in an implementation-defined value or a signal in C. Overflow-related undefined behaviors that can still occur in signed integer arithmetic are not relevant to this question, since they are not really related to "setting" a variable value. One can still note though that trying to set an int variable to an overly large floating-point value results in UB. Although even here one can argue that the UB occurs as part of the conversion, which happens before the actual "setting". -- Best regards, Andrey
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2022-12-21 12:02 +0100 |
| Message-ID | <tnup4t$10r05$1@dont-email.me> |
| In reply to | #88167 |
On 21/12/2022 06:31, Andrey Tarasevich wrote: > On 12/20/2022 12:46 PM, Andrey Tarasevich wrote: >> On 12/18/2022 12:00 PM, Roman P wrote: >> >>> 3) What happens when you overflow an int variable, that is, set it to a >>> value beyond its range? >> >> "Set"? What does "set" mean in this context? >> >> Behavior of variables in out-of-range situations depends critically on >> _how_ you attempt to produce an out-of-range value in it. For example, >> assignment is one thing, while side effect of `++` is a completely >> different thing. >> > > ... although here I'm probably trying to be too smart for my own good: I think we are all trying to be "smart" here - whether we are being /too/ smart is an open question! > there's no need for this kind of branching. In the end, side effects > modify the variable value through assignment as well. > > This means that trying to "set an int to a value beyond its range" > results in a congruent value in C++ and in an implementation-defined > value or a signal in C. Overflow-related undefined behaviors that can > still occur in signed integer arithmetic are not relevant to this > question, since they are not really related to "setting" a variable value. > > One can still note though that trying to set an int variable to an > overly large floating-point value results in UB. Although even here one > can argue that the UB occurs as part of the conversion, which happens > before the actual "setting". > I think there are three things to consider here. First, there is the issue of what the OP meant. Then there are the two different ways to set the value of a variable. Regarding the OP's intention - while this is speculation, I believe he was thinking of signed integer overflow in arithmetic expressions - overflow in the addition part of "x = a + b;" rather than in the assignment part. And in that case, signed integer overflow is undefined behaviour in C and C++, and many compilers optimise on the assumption that it never happens. (A few compilers, like "gcc -fwrapv", give it defined behaviour.) Setting a variable explicitly (whether it be by initialisation, assignment, augmented assignment, or increment/decrement operator side-effects) is a matter of conversion. For an integer type to an int, this is covered in 6.3.1.3. If the value to be converted cannot be represented in the new type (int, in this case), "either the result is implementation-defined or an implementation-defined signal is raised." For almost all compilers, excluding sanitizers or special debug modes, this is done by two's complement wrapping. Conversion from a floating point number that is out of range is undefined behaviour. The other way to change a variable is using memcpy, a character pointer, or a union. These methods affect the underlying representation directly, and do not operate on values - so "overflow" or "range" makes no sense. You can get undefined behaviour later when the value is read, if it is a trap or otherwise does not represent a valid value of the type. (You won't see that with integers, in real-world compilers - but you can see it if you set a _Bool variable to something other than 0 or 1 using memcpy() or a char pointer.)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web