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;
}