-1
#include<iostream>

int* function() {
    int a = 2;
    return &a;
}

int main() {
    int* b = function();
    *b = 3;
    return 0;
}

I was trying to run this program, but this is supposed to give me an error because a should be deleted after leaving the function. However, I received no error in Visual Studio 2022. a is not even a static variable, nor is b constant.

I tried to copy and paste this code in different compilers to see if the lecture was wrong, but I recieved

main.cpp:4:16: warning: address of local variable ‘a’ returned [-Wreturn-local-addr]

like what it is supposed to be.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
lEmonade
  • 1
  • 1
  • You need stricter compiler options, e.g. -Werror in GCC, /WX in msvc(Visual Studio), to report the warning as error. – o_oTurtle Aug 10 '23 at 01:41
  • 2
    A variable going out of scope is a logical error, not a syntax error. The compiler doesn't have to tell you anything. But if you turn the warnings up high enough you typically get a warning. Note that's a warning not an error, so the program will srill compile unless you take the further steps described in the comment above and turn the warnings into errors. This is a good thing to do wile learning because at this stage of your career most of the warnings you see will be lethal and not to be ignored. – user4581301 Aug 10 '23 at 02:03
  • [Here is visual studio emitting a warning and GCC with an extra option turned on to emit extra runtime diagnostics](https://godbolt.org/z/osGbjq8G6). `-fsanitize=address,undefined` spots the mistake and kill the program, giving you a nice backtrace to help track down the mistake in less trivial cases. Very handy and should be showing up in Visual Studio before long (if it's not there already). Also note that it's worth turning on the optimizer (`-O3`) because when the compiler optimizes it takes a longer, harder look at the code and may see more problems than it sees in the debug build. – user4581301 Aug 10 '23 at 02:09
  • Does this answer your question? [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – jabaa Aug 10 '23 at 02:09
  • Not this time, @jabaa . Asker knows it's a problem but doesn't know why they didn't get an error message (because it's technically not an error). – user4581301 Aug 10 '23 at 02:11
  • 1
    @user4581301 The question is unclear (actually, there is no question). The duplicate explains why there is no error. If that's not the answer, there should be clarification in the "question". – jabaa Aug 10 '23 at 02:12
  • 4
    Your code has undefined behaviour due to `function()` returning the address of a variable of automatic storage duration (`a`) which ceases to exist as the function returns. `main()` therefore receives a dangling pointer (a pointer to something that no longer exists) and then uses that pointer to modify the non-existent something (another instance of undefined behaviour). One property of undefined behaviour, according to the standard, is "no diagnostic required" but most compilers (in this specific case, but not others) can be *configured* (e.g. using command line option) to give a warning. – Peter Aug 10 '23 at 02:53
  • 1
    *but this is supposed to give me an error because a should be deleted after leaving the function*. This is incorrect. Your program **has** an error, for the reasons you explain. But this does **not** mean that compiling and/or running the program will give an error. That is your mistake. – john Aug 10 '23 at 06:54
  • C++ does not work the way you expect. There are *many* things in C++ that are syntactically valid but still illegal for you to do and result in [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub) and contrary to what many beginners tend to believe, the compiler is *not* required to diagnose when you do something that would invoke UB (and in many cases it simply cannot). – Jesper Juhl Aug 10 '23 at 07:39
  • If you use "Link time code generation" in Visual Studio, it doesn't warn when just compiling the source, but the warning comes when trying to build the executable. Apparently that is the time when the data flow analysis kicks in. – BoP Aug 10 '23 at 08:40
  • @lEmonade Did the answer solve your problem? – Minxin Yu - MSFT Aug 14 '23 at 07:47

1 Answers1

1

You should receive warning: C4172: returning address of local variable or temporary: a, in Visual Studio.

And MSVC doc Compiler Warning (level 1) C4172 treats it as a Compiler Warning. So you don't get an error by default.

Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14