C++ - freeing dynamic memory

Started by
19 comments, last by Khatharr 10 years, 12 months ago
Another example:
int* ptr1=NULL, ptr2=NULL, ptr3=NULL, ptr4=NULL;
ptr1=new int;
ptr2=ptr1;
ptr3=ptr2;
ptr4=ptr3;
delete ptr4;


Am I freeing ptr1-3, when freeing ptr4?



From what I read in C++ Primer Plus this wouldn't work since ptr2,ptr3, and ptr4 are not considered pointers but ordinary int variables, the statement would have to be:
int* ptr1 = NULL, *ptr2 = NULL, *ptr3 = Null, etc...
Advertisement

That is correct LordRhys, looks like everyone else missed that, have a cookie.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley


int* ptr1=NULL, ptr2=NULL, ptr3=NULL, ptr4=NULL;
ptr1=new int;
ptr2=ptr1;
ptr3=ptr2;
ptr4=ptr3;
delete ptr4;

Thankfully, this invalid C++ code will not compile, because it's invalid.

It's invalid because you can not assign an rvalue of type "pointer to int" to an lvalue of type "int", and because there is no overload of ::operator delete on type "int".

Stephen M. Webb
Professional Free Software Developer

That is correct LordRhys, looks like everyone else missed that, have a cookie.

Yes, when I saw the `int* blah' being used, I thought of pointing out how this is a bad idea because `int* blah, foo' declares a pointer and an int. But I didn't realize that we already had an example of that mistake.

If you use `int *blah' you are less likely to make this mistake. But of course the compiler will catch it the vast majority of the time.

Ooh, religious debate time!

I always use int* foo; since int* is the type. I never declare more than one pointer on a single line though.

But this is a religious debate, no point arguing, it's like asking which is better: Sunni or Shia. One had a big nose until she had plastic surgery and became a film star, the other became a politician and died after skiing into a tree. "I've got you babe" was a good song, however.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Ooh, religious debate time!

Sure, I haven't engaged in any of those in a while. :)

I always use int* foo; since int* is the type.

And I guess you would like to declare arrays as `int[8] a', since `int[8]' is the type. Unfortunately, declarations in C/C++ are not always as simple as `<type> <variable_name>'.

Like I said, I'm not going to argue. I just wanted to post my poor Sonny & Cher joke.

Note both Java and C# use int[] foo; to declare arrays, much more sensible.

And in C#

int* foo, bar;

actually does declare 2 pointers to int. Confusing (for C/C++ programmers), but consistent.

There's a lot of things I'm sure they would have done differently had C been invented later on (I'm looking at you, precendence of logical operators).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

And in C#

int* foo, bar;

actually does declare 2 pointers to int. Confusing (for C/C++ programmers), but consistent.

If you need some consistency: given int *foo, bar; then both *foo and bar are of type int.

I suppose. Who is better though, Sonny or Cher?

At least we can all agree a holy war should be declared (or defined) upon people who do this

int * foo, bar;

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

At least we can all agree a holy war should be declared (or defined) upon people who do this

int * foo, bar;

">Splitters!

This topic is closed to new replies.

Advertisement