2

Lets say, I have one pointer

int *l1 = new int[100*100];

int *l2 = l1;

Now, l1 & l2 both point to the same sequence of integer. I store some values into them.

Lets say, I do

delete [] l1;

Is l2 also deleted?

If I print the elements of l2 using

for(int i=1; i<=10000; i++){

 cout<<l2[i]; 

}

Will those values be printed alright?

2
  • When you say delete x or delete [] x the variable you name doesn't matter. What matters is the value. For example say int *x = new int; happens to set x to 0x12341234; now if the code contains delete (int*)0x12341234; later on then the memory is correctly deallocated. (And of course if the value of x isn't 0x12341234 then the program is broken.) Commented Oct 22, 2013 at 16:20
  • This is a fundamental complexity with pointers. I've found it helpful to use std::shared_ptr and also to set the value of pointers to NULL after they are deleted so I know (or get a better error) when they are incorrectly accessed after deletes. Commented Oct 22, 2013 at 17:24

4 Answers 4

21

i2 still points to the same place, but that memory has now been freed. Trying to access anything through it is undefined behaviour, which means your values may be printed, or your house may catch on fire. It's not up to you.

To be pedantic, neither i1 nor i2 are deleted, but the memory they pointed to is. They still retain their same values, but the memory there can now be reused for other things. It's unsafe to use a pointer that has been passed to delete because the memory might already have something else there.

Sign up to request clarification or add additional context in comments.

2 Comments

It's not "pedantic". It's something that you must understand in order to use pointers.
what about setting l2=NULL?
5

Here's an analogy which may help you understand.

A pointer is like the address of an apartment building.

Many people may have the address of the same building.

If one of them changes the array (think of renovating the building) everyone will see it as they are all looking at the same thing.

When you 'delete' the array you mark the apartment building as condemned and available for re-development.

Many people may continue to have the address of the now condemned building after it is deleted.

If they reference the array after it's deleted (the building was condemned) it may, or may not, still be present. It depends on if the city got around to demolishing the condemned building. It's not safe to do but there's nothing preventing you from doing it. Sometimes you get lucky and the building is still there. Sometimes you'll find something new was built in that place. Sometimes the building falls down on you.

1 Comment

You get lucky when the building is not there. I'm lucky when UB is a crash instead of a hidden bug.
2
int *l1 = new int[100*100]; 

With this statement, Integer array of 100*100 is created, i.e. (10,000 *4 bytes). In C int is 4 byte. enter image description here

With statement,

int *l2 = l1; 

enter image description here

delete [] l1; 

enter image description here

Comments

0

I2 is not deleted. I2 is now a pointer pointing to a place where you have no idea what is stored. This is because delete[] I1 deletes the variable I1 references not the pointer itself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.