Jump to content
  • Advertisement
Sign in to follow this  
romanMagyar

what's the relationship between const pointers and dyn. mem alloc?

This topic is 5147 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

My professor is stressing the importance of understanding const pointers before he can teach dynamic memory allocation. However, how are the two related?

Share this post


Link to post
Share on other sites
Advertisement
I wouldn't say they are directly related, but maybe he/she is probably trying to get to see that in c/c++ the name of an array is logically equivalent to a constant pointer to the first element of the array, the c/c++ standard allows you to use a pointer to an array and the name of an array as the same so you can use the name of an array just like a pointer and you can use a pointer to an array just like an array name.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
I wouldn't say they are directly related, but maybe he/she is probably trying to get to see that in c/c++ the name of an array is logically equivalent to a constant pointer to the first element of the array, the c/c++ standard allows you to use a pointer to an array and the name of an array as the same so you can use the name of an array just like a pointer and you can use a pointer to an array just like an array name.



That often doesn't apply to dynamic arrays.

int* pint = new int[10];
++pint; // valid

I suppose you could do

int* const pint = new int[10];

But that would prevent you from reassigning the pointer, though that could be good practice in some circumstances.

Share this post


Link to post
Share on other sites
Quote:
Original post by elementary
Quote:
Original post by snk_kid
I wouldn't say they are directly related, but maybe he/she is probably trying to get to see that in c/c++ the name of an array is logically equivalent to a constant pointer to the first element of the array, the c/c++ standard allows you to use a pointer to an array and the name of an array as the same so you can use the name of an array just like a pointer and you can use a pointer to an array just like an array name.



That often doesn't apply to dynamic arrays.

int* pint = new int[10];
++pint; // valid

I suppose you could do

int* const pint = new int[10];

But that would prevent you from reassigning the pointer, though that could be good practice in some circumstances.


I should have been more clear i was referring to statically allocated arrays.

Share this post


Link to post
Share on other sites
Quote:
Original post by romanMagyar
My professor is stressing the importance of understanding const pointers before he can teach dynamic memory allocation. However, how are the two related?


They are indirectly related in that if you ask for a dynamic memory allocation, you will get back a pointer to that memory, and you have to clean that memory up when you're done with it, which involves feeding the pointer back to the system (so it knows which bit of memory you want it to clean up). If you changed the pointer value in the meantime, boom. So const-ness lets you make sure at compile-time that you aren't going to change the pointer value.

Of course, it can be useful to have a pointer to somewhere in the middle of your chunk of dynamic memory, for some algorithms. You'll probably want to make a separate one, though, unless you are prepared for the headache of keeping the thing reset and making sure it is reset when you try to clean up. Trying to deallocate memory starting at the middle of an allocation = Bad News. Segfault city.

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!