Nooby pointer question

Started by
15 comments, last by mylifemysoul 14 years, 1 month ago
Quote:Original post by jpetrie
Quote:Original post by MARS_999
jyk look here

link


That thread is ta0lking about something else. The code you pasted above is still wrong:
int count = rand() % 1000;A **array = new A*[count]; // HERE you allocate array with new[].for(int i = 0 ; i < count  ; ++i) {   array = new A(); // HERE, array is allocated with new.}// ...for(int i = 0 ; i < count  ; ++i) {   delete []array;  // HERE, array is released with delete[] (WRONG)}delete []array; // HERE, array is released with delete[] (correct).


What you allocate with new, you release with delete. What you allocate with new[], you release with delete[]. Mix them, and you are wrong.

I assume you were referring to MikeP's correction of your first post in that linked thread; in that thread, you are allocating with new[] in one place and freeing with delete in another. In this thread you are doing exactly the opposite. Both are incorrect.


What are you talking about, first of all that code is his code I pasted from the rip-off

He does new with []

A **array = new A*[count];

Looks like a 2d array to me. So you will need to delete[]array[]

Unless I am missing something
Advertisement
Quote:
What are you talking about, first of all that code is his code I pasted from the rip-off

The code in my last post was the code you changed. Look at your first post in this thread, you say that "(some block of code) should be (some other block of code)". That second block of code is what I used in my previous post. That code differs from the original code rip-off provided in that you used the delete[] syntax inside the second loop, whereas rip-off used the delete syntax. He is correct, because the equivalent object from the first loop was allocated with new (not new[]).

Quote:
He does new with []

A **array = new A*[count];

Looks like a 2d array to me. So you will need to delete[]array[]

You will need to delete[] array. Not "delete[] array[]" -- that's not legal syntax at all and would not compile. It's not the deallocation of "array" you have done incorrectly -- in both rip-off's original code and your edited code, "array" is allocated with new[] and released with delete[].

What you did wrong was change the delete inside the second loop from a scalar delete to an array delete[]. That is wrong. You made no other changes to rip-off's code except to remove extra whitespace in the very last line ("delete [] array" became "delete []array" which is a superficial change that has no impact on the behavior or correctness of the program.
Quote:Original post by MARS_999
Looks like a 2d array to me. So you will need to delete[]array[]

Unless I am missing something

I do not believe 2D arrays use the syntax we are discussing. Consider this:
    typedef int* T;    T* array = new T[N];
To clarify, array is an array of pointers to other arrays. Replacing T with int* does not change how the code is interpreted.

EDIT: OK, you could call that a 2D array, but it doesn't have to be. The T's in array could point to pointers of singular integers, it could point to arrays of varying lengths, or anything you can imagine.
Quote:I do not believe 2D arrays use the syntax we are discussing. Consider this:
typedef int* T;

T* array = new T[N];
To clarify, array is an array of pointers to other arrays. Replacing T with int* does not change how the code is interpreted.

No, 'array' is a pointer to T. T is a pointer to int. 'array' is a pointer to a pointer to int. It is not an array of pointers to pointers (that would require three levels of indirection, here we have only two).

This is a 2D array:
int a[2][2];

However, because of the overwhelming tendency for arrays to decay to pointers in C++, we often talk about double-pointers as "being 2D arrays," even when this is not strictly correct. Because of this we can also say that 'array' is an array of pointers -- taking array and dereferencing it would yield an int*. You could even say that it is an array of arrays, if you wanted, and nobody would really fault you much. But it's not an array of pointers to arrays.

Array and pointer are both terms of indirection, and the number of terms of indirection in the English name of a type must match the number of terms of indirection in the syntactical representation of the type -- in other words, the number of stars ('reference' and & also apply, but not to this particular discussion).

Regardless, it does not change the fact that allocating with scalar new and releasing that allocation with array delete -- as MARS_999's example did -- is wrong.
Quote:Original post by jpetrie
Quote:I do not believe 2D arrays use the syntax we are discussing. Consider this:
typedef int* T;

T* array = new T[N];
To clarify, array is an array of pointers to other arrays. Replacing T with int* does not change how the code is interpreted.

No, 'array' is a pointer to T. T is a pointer to int. 'array' is a pointer to a pointer to int. It is not an array of pointers to pointers (that would require three levels of indirection, here we have only two).

This is a 2D array:
int a[2][2];

However, because of the overwhelming tendency for arrays to decay to pointers in C++, we often talk about double-pointers as "being 2D arrays," even when this is not strictly correct. Because of this we can also say that 'array' is an array of pointers -- taking array and dereferencing it would yield an int*. You could even say that it is an array of arrays, if you wanted, and nobody would really fault you much. But it's not an array of pointers to arrays.

Array and pointer are both terms of indirection, and the number of terms of indirection in the English name of a type must match the number of terms of indirection in the syntactical representation of the type -- in other words, the number of stars ('reference' and & also apply, but not to this particular discussion).

Regardless, it does not change the fact that allocating with scalar new and releasing that allocation with array delete -- as MARS_999's example did -- is wrong.


Ah yes I see now I had tunnel vision I just couldn't see that he only used new A() I just couldn't see that until now... My bad! Sorry

EDIT: Responded to the wrong argument. I have nothing to say.
omg, didn't realize that my post had so many replies.

Thanks for all the discussions.

Thought the first reply was correct and went ahead and used part of it.

Anyway, I used 2D vector<> to avoid the new and delete for ** pointers. It turned out to be working quite well.

This topic is closed to new replies.

Advertisement