Sign in to follow this  

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

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

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

This topic is 4839 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this