0

I'm a little bit confused by the following code

int main()
{
    int* a = new int{12};
    int* b = new int;

    b = a;
    delete a;

    delete b;

    return 0;
}

The code returns an error that

a.out(27538,0x10ade7e00) malloc: *** error for object 0x7f8c18504160: pointer being freed was not allocated
a.out(27538,0x10ade7e00) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      ./a.out

My question is that, when I delete the a, does it automatically delete b? What is the mechanism here, I'm a little bit getting lost.

3
  • 3
    You delete one pointer twice and leak the other. Commented Feb 16, 2021 at 22:24
  • 3
    Both a and b point to the same memory. Deleting both causes you to delete the same memory block 2 times which is undefined behavior. You also leaked the memory that b originally pointed to. There is no way to delete that because the address was thrown away in the b = a; assignment. Commented Feb 16, 2021 at 22:24
  • You are making the mistake of looking at the variable name instead of looking at the variable value. If you debugged the code, you would see that after those calls to new, both a and b have different values. It is those values that matter, not the name. When you issued the delete call, you are deleting the same value twice since you assigned the value of a to b. Commented Feb 16, 2021 at 22:29

3 Answers 3

8
b = a;    // here the int that b pointed at "leaks" (you have no way of deleting it)

delete a; // here "a" is deleted first

delete b; // here "a" is deleted a second time (undefined behavior)

When you assign the value of a to b, the value (address if you will) b previously contained, is forgotten. Both a and b then points at the same object. You then lost all possibilities to delete the original object b pointed at. When you then delete b you in fact try deleting a a second time.

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

2 Comments

I'd say that b = a; is the line where "b" leaks.
@eerorika I can buy that. I choose "lost" but I it's probably more correct to say "leak" directly there.
2

Let's walk through the code line by line.

int* a = new int{12};

This creates a new variable called a of type int* and initializes it to point to a newly-allocated integer assigned the value 12.

int* b = new int;

This creates a new variable called b of type int* and initializes it to point to a newly-allocated integer.

b = a;

This changes b's value to point to a. The value returned in the second call to new is now lost and that memory is leaked since it is no longer possible to pass it to delete.

delete a;

This deletes the object a points to, the one allocated first.

delete b;

Oops, this tries to delete the object b points to, but b doesn't point to any object that currently exists. The one allocated first was just deleted and no pointer to the second one exists. So this is a bug.

I suspect you are thinking that delete a; deletes a. It does not. It deletes whatever object a points to and requires that a be a pointer and point to a valid object that was allocated with new.

Comments

0

When you delete a pointer, all pointers, references, iterators etc. pointing to the destroyed object are invalidated. Because of the assignment b = a, b points to the same object as a does, so when a is deleted, b becomes invalid. Deleting an invalid pointer has undefined behaviour.

Note that b = a makes it impossible to delete the allocation that b previously pointed at. This is called a memory leak.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.