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

Started by
5 comments, last by Zahlman 19 years, 7 months ago
My professor is stressing the importance of understanding const pointers before he can teach dynamic memory allocation. However, how are the two related?
Advertisement
They arn't related.
The only thing I can think of is explained here, in the C++ FAQ Lite.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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.
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.
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.
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.

This topic is closed to new replies.

Advertisement