Jump to content
  • Advertisement
Sign in to follow this  
mylifemysoul

Nooby pointer question

This topic is 3036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites
Quote:
Original post by MARS_999
*** Source Snippet Removed ***


Should be

*** Source Snippet Removed ***
Why's that?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!