Pointers with runtime allocation - c++

I don't get that why reassigning a value to *p shows error. We've deleted it's resources, created at run time.
int a = 7, b = 8;
int *p = new int;
*p = a;
cout<<*p<<" "<<p<<endl;
delete p;
cout<<*p<<" "<<p;
*p = &b; // error
cout<<"\n"<<*p<<" "<<p;

The behaviour on dereferencing p after you've called delete p; is undefined. Don't do that. That's the easy bit. Now for the harder bit: the behaviour on reading the value of p (let alone dereferencing it) following a call to delete is undefined too! So don't do that either. (Informally you put the pointer back to an uninitialised state.)
*p = &b; is a typo. You need p = &b; to assign the pointer p to the address of b. If you fix that typo, you are free to read the value of p and dereference it once again.

The problem besides deleting the pointer and not giving it a new address is currently you are trying to assign the value of the p pointer to a memory address of an int.

Related

What happens to values on the stack that get placed in array on the Heap

I want to know if I create an array on the Heap in C++ and I assign data from the stack to the Array, what exactly happens? Is the data copied onto the heap?
code fragment:
int a = 1;
int b = 2;
int *c = new int(3);
int *arr = new int[3];
arr[0] = a;
arr[1] = b;
arr[2] = *c;
int a = 3;
a has the value 3.
the memory address of a is 0x345A or something like it.
a is on the stack.
int *p = &a;
pointer p is assigned the address of a. The memory address of p is 0x123B or something like it. That memory address holds the value 0x345A pointing to the location where the value 3 is stored. Pointer p is on the stack.
You'd get 3 if you print a and *p since they are getting their value from the same memory location.
You'd get 0x345A if you print &a and p since a's address is the value p holds.
You'd get 0x123B if you print &p since that is the memory location for pointer p.
Assuming *c points to an integer on the heap, you could store the value of c (which is the memory address of the integer on heap) in an array.
int *arr = new int[3];
...
arr[2] = c;
What you do NOT want to do is:
int foo = 10;
arr[2] = &foo;
Now, your array is pointing to a memory location that that is on the stack. Will work OK as long as you are in that function, but will be pointing at garbage once you exit the function and stack is reclaimed.

Operator precedence and pointer arithmetic

Given the following code:
void Allocate(int *p)
{
p = new int;
*p++ = 2;
}
int main()
{
int i = 10;
Allocate(&i);
std::cout << i << std::endl;
}
I'm a bit confised about the meaning of:
*p++ = 2;
The output is 10 and my reasoning as to why this is the case is that *p++ is a temporary therefore any assignment to it is lost at the end of the scope of Allocate(int *p).
Is this the case?
Thanks in adv!
On input to Allocate, p points to the variable i in the main
function.
The address of this variable then lost and replaced by the
new int.
The value of this int (which is uninitialized and so could
start as anything) is set to 2.
The p pointer is incremented.
The Allocate function returns at this point, leaking the int that was
allocated.
The value of i in the main function is unchanged,
because Allocate did not modify it.
when you pass the the address of i into Allocate, another (temp) pointer is created that points to i's address (i.e. passing by pointer). then that temp pointer is pointed to a new location (via new int). thus the value of i is left alone.
p = new int;
You're assigning p new memory to point to instead of what it was pointing to before. You then change this newly allocated memory and it's lost forever when the function ends, causing a memory leak. If you remove the allocation line, it should cause an output of 2. The ++ does nothing in this case. It just increments the pointer and returns the old value to dereference.
As soon as you enter Allocate, you assign p to point to a new block of memory, so it no longer points to i. Then you modify that new block of memory (which is then leaked when the method returns.) i is unaffected because you've moved that pointer before you set the pointed-to memory cell.
void Allocate(int **p)
{
*p = new int;
**p = 2;
}
int main()
{
int j = 10;
int *i = &j;
std::cout << i << std::endl;
Allocate(&i);
std::cout << i << std::endl;
}
Output is :
10
2
You need a pointer to pointer to change the address of the location being pointed to.

Placement new for block of pointers

i am trying to allocate pointers of the first order one by one at memory.
Pointer of the second order must walk through this queue and handle to objects, created on that pointers.
Placement new works fine when i allocate the queue of objects(not pointers to them), but when i am trying the same with pointers the trouble happens.
int* p;
p = new int(1);
printf("%p - %d\n", p, *p); // address of int and its value
delete p;
First object is created and deleted to initialize pointer with some address, next objects will be created in cycle
p = new(p) int(2); // (p) - memory will be allocated at address
// on which p points
printf("%p - %d\n", p, *p); // address of int and its value
delete p;
p++;
p = new(p) int(3); // (p) - memory will be allocated at address
// on which p points
printf("%p - %d\n", p, *p); // address of int and its value
delete p;
Output:
01C1FFD0 - 1
01C1FFD0 - 2
01C1FFD4 - 3;
int** pp;
pp = new int*;
printf("%p\n", pp); // address of int*
*pp = new int(1);
printf("%p - %d\n", *pp, **pp); // address of int and its value
delete *pp;
delete pp;
pp = new(pp) int*;
printf("%p\n", pp); // address of int*
*pp = new int(2);
printf("%p - %d\n", *pp, **pp); // address of int and its value
delete *pp;
delete pp;
pp++;
pp = new(pp) int*;
printf("%p\n", pp); // address of int*
*pp = new int(3);
printf("%p - %d\n", *pp, **pp); // address of int and its value
delete *pp;
delete pp;
If int is not created by *pp then if pp is incremented it is not real to delete pp later,
if int is created by *pp then there is something strange with addresses and second value.
Output:
01C1FFD0
01C1FFE0 - 1
01C1FFD0
01C1FFD0 - 29491152
01C1FFD4
01C1FFD0 - 3
I would be very grateful for advice how to organize this queue correct.
p = new(p) int(2); // (p) - memory will be allocated at address
// on which p points
This is where you're wrong for the first time. new(p) T does not allocate any memory. Rather than that, it just creates a T at address p, assuming that p points to some memory where a T can be constructed. Also, since there is no memory allocated, you cannot just pass such pointers to delete, as this will attempt to release the not allocated storage. Rather than that, you need to manually invoke the destructor and dispose of the memory depending on however you acquired it.
Placement new is a rather advanced concept that you very likely do not need at this time. In fact, even "simple" manual memory/object lifetime management is something you probably should not be bothering yourself with at this time of your learning. What book are you learning C++ from? Have you seen this book list?
FYI: I haven't looked at your code below that line.

Updating map values in c++

Newb question here:
how can I have the value stored in Maptest[2] update along with the variable?
I thought you could do it with pointers, but this doesn't work:
map<int, int*> MapTest; //create a map
int x = 7;
//this part gives an error:
//"Indirection requires pointer operand ("int" invalid)"
MapTest[2] = *x;
cout << MapTest[2]<<endl; //should print out 7...
x = 10;
cout <<MapTest[2]<<endl; //should print out 10...
What am I doing wrong?
You need the address of x. Your current code is attempting to dereference an integer.
MapTest[2] = &x;
You then need to dereference what MapTest[2] returns.
cout << *MapTest[2]<<endl;
Try this instead:
MapTest[2] = &x;
You want the address of x to store in the int*. Not the dereference of x, which would be what ever is at the memory location 0x7, which is not going to be valid.
There are at least 2 problems here:
int x = 7;
*x; // dereferences a pointer and x is not a pointer.
m[2] = x; // tries to assign an int value to a pointer-to-int value
// right
m[2] = &x; // & returns the address of a value
Now you have a new problem. x has automatic lifetime, it will be
destroyed at the end of its surrounding scope. You need to allocate it
from the free store (a.k.a. the heap).
int* x = new int(7);
m[2] = x; // works assigns pointer-to-int value to a pointer-to-int value
Now you have to remember to delete every element in the map before
it goes out of scope or you will leak memory.
It is smarter to store values in a map, or if you really need to
store pointers to store a suitable smart pointer (shared_ptr or
unique_ptr).
For the printing:
m[2]; // returns pointer value
*m[2]; // dereferences said pointer value and gives you the value that is being pointed to

Pointers problem

I have a question.
I created a correctly initialized integer pointer
int * p
and a correctly initialized integer array
int * array1 = new int[]
Which of the following is legal code?
p = array1;
array1 = p;
Or are both correct?
is this possible as well
p[0] since, to my pointer arithmetic knowledge, it doesn't add anything.
All in c++
If the question is trying to get at pointers versus arrays, they are not always compatible. This is hidden in the presented code because the array is immediately converted to a pointer.
int* array1 = new int[5]; // Legal, initialising pointer with heap allocated array
int array2[5] = {0}; // Declaring array directly on the stack and initalising with zeros
int *p = 0; // Declaring pointer and initialising to numm
p = array2; // Legal, assigning array to pointer
p = array1; // Legal, assigning pointer to pointer
array1 = p; // Legal, assigning pointer to pointer
array2 = p; // ILLEGAL, assigning pointer to array
Here array2 has an array type and cannot be used to store a pointer. Actually, the array cannot be reassigned at all, as it is not an l-value.
int array3[5] = {0}; // Declaring array directly on the stack and initalising with zeroes
array3 = array2; // ILLEGAL, array not an l-value
The array has a fixed address and reassiging it would be similar to trying to write:
int i = 0;
&i = p;
[Hopefully, trying to reassign the location of a variable is obvious nonsense.]
Both are legal.
The first, "p = array1", will cause your correctly initialized integer pointer to be leaked and to point p to the first occurrence of the array that array1 points to.
The second, "array1 = p", will cause the correctly initialized integer array to to be leaked and to point array1 to the single int that p points to.
So I suspect I'm missing something. Could you perhaps post complete code?
If both p and array1 are declared as "int *", as you imply here, then either assignment is legal, by definition.
both are leagal.
passing adresses to eachother. dont knw what u want to do
According to the explanation provided in this site. The first assigned in is correct but the second assignment cannot be considered valid. Please look under the title 'Pointers and arrays'.
You can assign your array variable to pointer variable. So p = array1 is correct.
You can refer this code.
#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
for (int n=0; n<5; n++)
cout << p[n] << ", ";
return 0;
}
If both are pointers then no need to declare pointer as array like int *array1 = new int[].
Both variables are of type pointer to int. So both assignments are legal. The fact that one of the variables is an array doesn't matter in C. Arrays and pointers are the same after initialization.
"I created a correctly initialized integer pointer int * p and a correctly initialized integer array int * array1 = new int[]"
First of all, keeping the syntactical mistakes aside, the terminology is wrong.
int *p; - Here you are just declaring a pointer to hold an integer variable's address. You are not initializing it. Declaration is different from initialization and initialization is different from assignment.
int *array1 = new int[]; - Keeping the error aside, this is not an initialized integer array. array1 is a pointer pointing to an array of integers.
And passing nothing to [] is incorrect. A value needs to be passed that decides number of memory locations to be allocated for holding integer values.
Answering the questions.
Which of the following is legal code?
p = array1;
If, array1 is properly initialized it's correct. p points to the first integer in the array1. Even if it is not properly initialized also, it is correct. In this case, both array and p points to garbage values.
int* array1 = new int[5]; // Notice **5** being passed.
array1 = p;
Infact this operation is useless. Both p and array1 were both pointing to the same location earlier. But this is legally correct.
is this possible as well p[0] ?
Yes, it's correct. p can dereference to 0 to 4 as it's index values. If you just want to p[0], in that case -
int* array1 = new int ; // Notice nothing being passed.