can anyone tell me what is wrong with this piece of code
int* x=new int(5) ;
int i =0;
int** y = new int*[i];
for(int j = 0 ;j<5 ; j++)
{
y[i++]=x;
}
delete[] y;
the compiler always triggers a breakpoint when I delete y note that I don't want to delete the object "x" thanks
int i =0;
int** y = new int*[i];
Well, you have just allocated an array of pointers to int that big enough to fit... zero elements. In your loop you are:
j.x to... every other element of y... that is outside the bounds of the array as previously mentioned.I don't really know what you are trying to accomplish here. How about a bit more background? You are invoking undefined behavior by assigning outside the bounds of y, so anything can happen after that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With