Nooby pointer question

Started by
15 comments, last by mylifemysoul 14 years, 1 month ago
Suddenly get a little big confused about pointers. Here are my questions: assume i defined a class A. Are the following all correct? And what's the difference? 1> A* a = new A(); delete a; 2> A* a=null; int num = 10; a = new A[num]; delete a[]; 3> A* a[10]; int num = 10; for(int i=0;i<num;i++) a = new A(); For 3> can I use something like A** a and according to 'num', dynamically create the array?
Advertisement
Apart from the fact that "null" isn't a valid value, yes they are all correct. However, #3 doesn't release the memory it allocates (you would need to loop over and delete each instance).

You can have a dynamically created array of pointers:
std::vector<A *> array;for(int i = 0 ; i < 10 ; ++i) {   array.push_back(new A());}// ...for(int i = 0 ; i < array.size() ; ++i) {   delete array;}

Using the double pointer syntax:
int count = rand() % 1000;A **array = new A*[count];for(int i = 0 ; i < count  ; ++i) {   array = new A();}// ...for(int i = 0 ; i < count  ; ++i) {   delete array;}delete [] array;
Quote:Original post by rip-off
Apart from the fact that "null" isn't a valid value, yes they are all correct. However, #3 doesn't release the memory it allocates (you would need to loop over and delete each instance).

You can have a dynamically created array of pointers:
*** Source Snippet Removed ***
Using the double pointer syntax:
*** Source Snippet Removed ***


Thanks a lot. It's very helpful.
Quote:Original post by rip-off
Apart from the fact that "null" isn't a valid value, yes they are all correct.


Not quite.

delete[] a[];

Also, there is almost never a reason to handle the allocations at this level. There are other, better reasons to understand pointers :)
int count = rand() % 1000;A **array = new A*[count];for(int i = 0 ; i &lt; count  ; ++i) {   array = new A();}// ...for(int i = 0 ; i &lt; count  ; ++i) {   delete array;}delete [] array;



Should be

int count = rand() % 1000;A **array = new A*[count];for(int i = 0 ; i &lt; count  ; ++i) {   array = new A();}// ...for(int i = 0 ; i &lt; count  ; ++i) {   delete []array;}delete []array;
Quote:Original post by MARS_999
*** Source Snippet Removed ***


Should be

*** Source Snippet Removed ***
Why's that?
Quote:Original post by MARS_999
*** Source Snippet Removed ***


Should be

*** Source Snippet Removed ***


Unless I see something wrong, no it shouldn't. You're getting an A* that points to a single element by the array access, because it was allocated as new A().

Anyway, serves as a good example why plain C pointers for array indexing in C++ is an unnecessary source of bugs.
Quote:Original post by Beyond_Repair
Anyway, serves as a good example why plain C pointers for array indexing in C++ is an unnecessary source of bugs.

But array indexing is defined with pointer arithmetic, so indexing through pointers is actually more natural than indexing with real arrays :)

x[y] is just syntactic sugar for *(x+y)
jyk look here

link
Quote:Original post by MARS_999
jyk look here

link


That thread is talking 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.

This topic is closed to new replies.

Advertisement