On 11/30/22 23:57,
gdo...@gmail.com wrote:
> On Monday, November 28, 2022 at 3:09:38 AM UTC-5, Juha Nieminen wrote:
...
>> It's not only to limit the visibility of the variable to the scope of
>> the conditional (which in itself is useful, don't get me wrong).
>> Sometimes you want the object in question to be destroyed at the end
>> of the conditional. Before you had to do it in an uglier way:
>> //---------------------------------------- code_code_code();
>> { Type obj = something; if(obj) { do_something_with(obj); } } //
>> 'obj' will be destroyed here
>> more_code_here(); //----------------------------------------
>> Now you can achieve the same thing easier:
>> //---------------------------------------- code_code_code();
>> if(Type obj = something; obj) { do_something_with(obj); } // 'obj'
>> will be destroyed herre
>> more_code_here(); //----------------------------------------
>
> hm? this was bad. I'm surprised. when the scope of the variable has
> been reached
> that space should be free and reclaimable.
An object with automatic storage duration does in fact get destroyed and
the memory deallocated when the end of its scope is reached. I'm curious
- which of the above examples appears, in your opinion, to be
inconsistent with that rule? Both of the comments that says "'obj' will
be destroyed here" is located on precisely the line where the scope of
'obj' ends.
> Questions, when a local variable(s) is created it is pushed on the
> stack? once the function is ended, isn't all the variables that were
> created
> removed from the stack?
No, each block in a program can have it's own automatically allocated
objects, and such objects should be destroyed and the memory they
resided in deallocated at the end of the block. That will be at the end
of the function only for the outermost block of the function.
Whether a stack of any kind is involved is outside the scope of the C++
language definition. It talks about object lifetimes and scopes, but not
stacks (except for std::stack<>, which is a different issue). On systems
that, for instance, use activation records rather than stacks, the same
rules apply, but their implications for how activation records are
created and destroyed are different from the implications for how stacks
are pushed and popped.
> Questions: when a function returns the address, the function
> terminates but it gave,
> returned an address. now the address return is a location, but the
> value there should not be considered valid right, right. I works out
> to be ok because the returned address had a type an given that address
> the system knows how many bytes are associated with that address and
> if not wrote to that address the data would be as last left, but
> really it is not usable. (unless it was global)
The object that used to reside in the location that the pointer points
at might have already been replaced by something else, which is why it's
undefined behavior to dereference such a pointer.
> when does a function end at the '}' or at the 'return ' because the
> return never let's it reach the ending '}' ? so are those locals still
> on the stack still on the stack?
A return statement ends all of the nested blocks that it occurs inside,
in reverse order of nesting. Objects with automatic storage duration
defined in those blocks will be destroyed and deallocated in that same
order.