2

The following code prints 0 with GCC 4.8.1 (why not) but 10 with MSVC2013 (why?):

#include <iostream>
#include <vector>

const int y = 10;

int main()
{
  std::vector<double> y(y);
  std::cout << y.size() << std::endl;

  return 0;
}
Korchkidu
  • 4,908
  • 8
  • 49
  • 69
  • 9
    Undefined behaviour. Both are right. – chris May 04 '14 at 19:08
  • 3
    Try clang: `t.cpp:8:25: warning: variable 'y' is uninitialized when used within its own initialization [-Wuninitialized]` at `std::vector y(y);` – Mat May 04 '14 at 19:09
  • @mat: clang seems to be the smart one^^. – Korchkidu May 04 '14 at 19:22
  • @chris: how could MSVC2013 use / find y=10 here? – Korchkidu May 04 '14 at 19:23
  • @Korchkidu Since this is UB MSVC can do what ever it wants with this code, including guessing you meant the global `y` and use it or impregnate your cat. – Baum mit Augen May 04 '14 at 19:25
  • @Korchkidu: undefined behavior - you can't reason about it. Might be picking up random cruft on the stack. Might be hardcoded to try and give you a value you never expect. – Mat May 04 '14 at 19:25
  • @Korchkidu, If it's ignoring the local and using global, that worries me a bit. Otherwise, as said, UB can mean anything, and Clang is often good at telling you about it. Anyway, if you'd like to test the validity of MSVC on the local vs. global thing, try running [this](http://coliru.stacked-crooked.com/a/81953c9b75dd6325). `p` should be the same output as `&p`. – chris May 04 '14 at 19:34
  • 1
    Neither gcc 4.7.2 nor gcc 4.9.0 give a single warning for this code even though I passed `-Wall -Wextra -std=c++11 -pedantic`. This is somewhat disappointing. :( By the way, the generated executable crashes as `std::bad_alloc` is thrown. – Ali May 04 '14 at 19:56
  • What does MSVC tell you with [`/W4`](http://msdn.microsoft.com/en-us/library/thxezb7y.aspx)? – Ali May 05 '14 at 14:43
  • @ali: MSVC tells nothing with /W4 nor /Wall... – Korchkidu May 06 '14 at 07:53
  • @Malloc: Indeed, as it is UB, MSVC can do whatever it wants. However, I would expect compiler to highlight UBs by returning completely wrong values instead of trying to "repair / hide" them;) These are design choices I have a hard time to understand actually;) Thanks! – Korchkidu May 06 '14 at 07:56
  • @chris: you are the most upvoted comment. Could you make it an answer with some additonal reference to the C++ standard (and I will accept it) please? – Korchkidu May 06 '14 at 07:58
  • @Korchkidu Thanks for the info! Yeah, it is quite disappointing. :( – Ali May 06 '14 at 08:52
  • 1
    @Korchkidu, I think there's a pretty relevant question [here](http://stackoverflow.com/questions/9820027/using-newly-declared-variable-in-initialization-int-x-x1), actually. – chris May 06 '14 at 11:47
  • @chris: while double y[y] is ok, std::vector y(y) is not. Most probably because the vector is first declared, and then initialized. This may be somehow related indeed. Thanks! – Korchkidu May 06 '14 at 12:03

1 Answers1

0

i think you need compile with warning level 4 or warning is an error in visual studio and
-Wall and -Werror or -Winit-self or -Wuninitialized for ggcc.
That compile, but you have an allocation problem.

For me the best flags are : -std=c++11 -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -Wl,-z,relro -Wl,-z,now -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Werror

Max
  • 1
  • 1