
On 22-Apr-2014 21:12, liamw wrote:
int main() { int *p; { int x = 0; p = &x; } return *p; }
I asked about this on the valgrind list and the response was that g++ does not actually allocate by block scope, but rather by function scope. So in terms of what the compiler produces, the preceding program is exactly the same as:
int main() { int *p; int x = 0; p = &x; return *p; }
The main difference is that in the original form the compiler can generate warnings/errors about direct use of out of scope variables. (But not indirect, as here.) I did the experiment to verify that this was indeed the case, compiling with
g++ -Wall -ansi -O0 -S test.cpp
and with and without the braces around
int x= 0; p = &x;
and the .s files were identical. (There are slight differences if -g is added, but that is because it is adding pieces for the debugger.)
So while I have always heard is that "C++ takes care of new and delete for you" inside block scope, the reality is that with g++, it does not (ever?) do that.
Regards,
David Mathog mathog@...1176... Manager, Sequence Analysis Facility, Biology Division, Caltech