Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change in default or value initialization from C++11 to C++14

61 views
Skip to first unread message

Paul

unread,
Nov 3, 2016, 9:09:51 AM11/3/16
to
On my home PC, I use C++11 with gnu and codeblocks.
The code below generates a garbage value which is what I would expect.

#include <vector>
#include <iostream>
int TenPower(std::vector<int> n) {
int r;
return r;
}
int main()
{
std::cout << TenPower({});
// Output is 4199040 -- not surprising.
}


However, when I use codefights.com which enables g++ 5.0 c++ 14, r = 0 is
returned. However, this website doesn't give users ready access to the
main statement or the #include statements.

It seems (to me) like the TenPower function returns 0 in C++14 and a garbage
value in C++11. Is this correct? If so, what is the precise change that
makes this difference?

If I'm not correct, could someone explain why 0 is consistently returned
on codefights.com?

Thank you.

Paul

David Brown

unread,
Nov 3, 2016, 9:32:03 AM11/3/16
to
The code has an undefined value. This means there are no guarantees of
any sort - it is quite possible that one version of gcc or one set of
flags returns 0 consistently, while another version of gcc and other
flags returns something else. It makes little sense to try to
investigate the details of compilation of clearly incorrect code.

Scott Lurndal

unread,
Nov 3, 2016, 9:56:11 AM11/3/16
to
Paul <peps...@gmail.com> writes:
>On my home PC, I use C++11 with gnu and codeblocks.
>The code below generates a garbage value which is what I would expect.
>
>#include <vector>
>#include <iostream>
>int TenPower(std::vector<int> n) {
> int r;
> return r;
>}
>int main()
>{
> std::cout << TenPower({});
> // Output is 4199040 -- not surprising.
>}
>
>
>However, when I use codefights.com which enables g++ 5.0 c++ 14, r = 0 is
>returned. However, this website doesn't give users ready access to the
>main statement or the #include statements.
>
>It seems (to me) like the TenPower function returns 0 in C++14 and a garbage
>value in C++11. Is this correct? If so, what is the precise change that
>makes this difference?

It's simply returning a random value from the stack. It could be anything,
and it simply depends on what had used that portion of the stack prior
(e.g. during the crt startup functions) and how the code was generated
and/or optimized.

Bo Persson

unread,
Nov 3, 2016, 12:26:44 PM11/3/16
to
On 2016-11-03 14:09, Paul wrote:
> On my home PC, I use C++11 with gnu and codeblocks.
> The code below generates a garbage value which is what I would expect.
>
> #include <vector>
> #include <iostream>
> int TenPower(std::vector<int> n) {
> int r;
> return r;
> }
> int main()
> {
> std::cout << TenPower({});
> // Output is 4199040 -- not surprising.
> }
>
>
> However, when I use codefights.com which enables g++ 5.0 c++ 14, r = 0 is
> returned. However, this website doesn't give users ready access to the
> main statement or the #include statements.
>
> It seems (to me) like the TenPower function returns 0 in C++14 and a garbage
> value in C++11. Is this correct? If so, what is the precise change that
> makes this difference?
>

Zero is also a garbage value, just as unusual as 4199040, or 42.



Bo Persson


JiiPee

unread,
Nov 3, 2016, 1:17:34 PM11/3/16
to
On 03/11/2016 16:26, Bo Persson wrote:
> Zero is also a garbage value, just as unusual as 4199040, or 42.


but its just that there are much more zeros in the storehouse than like
4199040 's ...

woodb...@gmail.com

unread,
Nov 3, 2016, 1:54:28 PM11/3/16
to
+1

Mr Flibble

unread,
Nov 3, 2016, 2:18:33 PM11/3/16
to
Please don't upvote here you cunt.

/Flibble


woodb...@gmail.com

unread,
Nov 3, 2016, 2:48:51 PM11/3/16
to
On Thursday, November 3, 2016 at 1:18:33 PM UTC-5, Mr Flibble wrote:


Please don't use abusive language here.


Brian
Ebenezer Enterprises

mark

unread,
Nov 3, 2016, 4:24:59 PM11/3/16
to
That's dead wrong. 'TenPower()' returns a timebomb of undefined
behavior. The return value has magic properties. It can assume any
number of values at once and doesn't obey the laws of logic.

E.g.:

------------------------------------------------
int test() {
int i = TenPower(std::vector<int>());
if((i+0) % 2 != 0) return 42;
if((i+1) % 2 != 0) return 43;
return 44;
}
------------------------------------------------

clang compiles this into:
------------------------------------------------
test(): # @test()
mov eax, 44
ret
------------------------------------------------


Louis Krupp

unread,
Nov 3, 2016, 6:03:35 PM11/3/16
to
No more sausages for you...

Louis

Alf P. Steinbach

unread,
Nov 3, 2016, 6:33:41 PM11/3/16
to
On 03.11.2016 14:09, Paul wrote:
> On my home PC, I use C++11 with gnu and codeblocks.
> The code below generates a garbage value which is what I would expect.
>
> #include <vector>
> #include <iostream>
> int TenPower(std::vector<int> n) {
> int r;
> return r;
> }
> int main()
> {
> std::cout << TenPower({});
> // Output is 4199040 -- not surprising.
> }
>
>
> However, when I use codefights.com which enables g++ 5.0 c++ 14, r = 0 is
> returned. However, this website doesn't give users ready access to the
> main statement or the #include statements.
>
> It seems (to me) like the TenPower function returns 0 in C++14 and a garbage
> value in C++11. Is this correct? If so, what is the precise change that
> makes this difference?

There's no change, it's still an indeterminate value.


> If I'm not correct, could someone explain why 0 is consistently returned
> on codefights.com?

It could be just coincidence, but g++ has a tendency to null out things.

As I recall it even does that for dynamically allocated memory.


Cheers!,

- Alf

Richard

unread,
Nov 3, 2016, 6:46:38 PM11/3/16
to
[Please do not mail me a copy of your followup]

> Please KILLFILE Mr. Flibble Cunt

+1
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Tom Allebrandi

unread,
Nov 4, 2016, 3:50:23 AM11/4/16
to
+1 for Brian's comment about not using vulgar language.

-1 for Fibble's use of vulgar language.

Note that downvoting is (probably) OK with Fibble since s/he only
complained about upvoting.


--- tom

Tom Allebrandi

unread,
Nov 4, 2016, 3:50:23 AM11/4/16
to
-1 to me for mispelling Flibble

red floyd

unread,
Nov 4, 2016, 12:18:44 PM11/4/16
to
Get the fuck off of your high horse already.

Lynn McGuire

unread,
Nov 4, 2016, 2:28:29 PM11/4/16
to
On 11/4/2016 2:48 AM, Tom Allebrandi wrote:
> -1 to me for mispelling Flibble

+1

Lynn
0 new messages