need help

How can the output be 2.2 when
child()
points to a invalid memory address by the time
child()
returns? Shouldn't the variable's data be destroyed too at that point?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double* child(void)
{
    double dLocalVariable = 2.2;
    return &dLocalVariable;
}

void parent(void)
{
    double* dVar = child();
    *dVar = *child();
    cout << *dVar << endl;
}

int main()
{
    parent();
}
Last edited on
Because this is undefined behavior: compiler can do everything, including returning expected value.

"Destroying" a variable is means running its destructor and freeing its memory. As primitive types destructor does nothing, it does not change its value.

If you access desrtoyed variable following can happens:
1) Memory where variable or some of its parts resided is returned to OS. Attempt to access it will cause crash.
2) Memory is still belongs to program and nothing touched it: you get old values stored there, you can write and read again and everyrthing will seem to work.
3) Memory is still belongs to program, but was overwritten: you will get something else for primitive types and can even crash program for complex types
4) Memory is given to other object: as (3), but writes to that memory will corrupt other objects.
5) Something else.

In your case (2) happens. Due to how program stack works and how compilers tend to generate code if optimisations are turned off, this is expected.

In one of PMs I sent a program which I claimed can output three different values. Did you try to compile it? It will show case (3): memory returned pointer points to gets overwritten.
How can it be 8 when i is not set to 8? (Your program from PM)

Memory where variable or some of its parts resided is returned to OS.

I don't know what you mean with that?

Or this:
Memory is still belongs to program


This is my first time learning about memory.
Last edited on
bump
The memory is available for reuse but not physically destroyed. If it has not been reused, it may work. But it is still undefined behavior. It could do anything and still be compliant with the language standard. One of those anything's is to print out the value that was assigned in the child function.
What about something like this? i gets the value of k?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int* foo() {
    int i = 3;
    return &i; 
}
void bar() {
    int k = 8;
}
int main() {
    int *i = foo();
    bar();
    std::cout << *i ;
}
If the particular compiler is using a stack for storing its automatic variables, then that is a possibility. However, one cannot depend on a compiler using a stack. (Stack is not used in the standards.) Especially since bar is returning void and foo is not. Or if they had different parameter lists, it would not work.

This is why the standard simply says undefined behavior so various conditions do not need to be defined. So do not depend on any particular behavior. Because it could change with a different set of options on the compiler, or a different version of the compiler, or a project team which decides to change compilers.

Topic archived. No new replies allowed.