0

What is the difference between *p_address= new int(2) and the assignment via & p_address = &value considering that both examples take place inside the function? For example: I've got the int pointer *original_pointer. I pass its address to the function. Inside the function I create an int pointer which points to the int value of 2. Then I assign the pointer (which is created inside the function) to the *original_pointer. When I cout the *original_pointer outside the function, it returns -858993460, while inside the function it returns the value of 2. However, when I use new to create a pointer inside the function, the value of *original_pointer inside and outside of the function is the same. Here is the code:

int main() {
    while (true) {
        void assign_(const int**);
        char* tmp = " ";
        int const *original_pointer;
        assign_(&original_pointer);
        cout << "the address of original_pointer is " << original_pointer << endl;
        cout << "the value of original_pointer is " << *original_pointer << endl;
        cin >> tmp;
    }
    return 0;
}
void assign_( int const **addr) {
    int* p_value;
    int value = 2;
    p_value = &value;
    *addr = p_value;
    //*addr = new RtFloat(2.0);   // If I create the pointer this way the value of *addr is the same with *original_pointer
    cout << "the adress of *addr inside the function is " << *addr << endl;
    cout << "the value of **addr inside the function is " << **addr << endl;
}
MonteNero
  • 143
  • 1
  • 6

3 Answers3

1

*p_address= new int(2) Allocates memory for 2 ints one int (with the value 2) which "lives" until you delete it.

p_address = &value just sets p_address to the address of a local variable which becomes invalid as soon as the function exits (as you've seen).

John3136
  • 28,809
  • 4
  • 51
  • 69
0

The difference between *p_address= new int(2) and the assignment via &p_address = &value; Is that in the first case the value pointed to by p_address is on the heap and in the second case it is on the stack. This means that the lifetime of the value pointed to by p_address in the first case is until it is deleted and in the second case it is only alive until value goes out of scope.

Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
  • *p_address= new int(2) is a actually a dynamic memory allocation integers on the heap memory. The program creates memory on heap. This basically means that addresses will remain valid till 1. Delete[] operator is used explicitly by program.2. The process execution stops. When this happens the OS will reallocate the memory to some other process. Most modern OS do this. p_address = &value is basically assigning the address of a variable to the pointer. This gets allocated on the stack. This means the variable will only be valid as long as it is within a scope. – Spanky May 19 '15 at 06:59
0

When you use int value = 2; it is created on the stack. Once the function returns the variable is automatically deallocated.

When you use *p_address= new int; the memory is allocated on the heap. it is not automatically deallocated. so even after the function returns that memory is there.

911
  • 908
  • 8
  • 16