Sign in to follow this  
iNsAn1tY

Quick const pointer question...

Recommended Posts

Evening all. Say I have a function in a class which creates an instance of an object using the new keyword, stores the pointer by some means (in a map or a vector or something), and returns a const pointer to the object (the return type of the function being const [ClassName] *). What happens if delete is called on the returned pointer? Is the instance of the object it points to destroyed, thereby rendering the pointer held by the class which created it invalid? I assume yes, but I was just wondering if someone can clarify this. Also, if the answer is yes, I assume that I should use something like boost::shared_ptr to avoid instances of objects being destroyed accidentally? Thanks in advance...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Actually deleting a const pointer is not legal in C++ so it shouldn't be a problem unless you're using a non-conforming compiler.

- Anonymous coward.

Share this post


Link to post
Share on other sites
deleting a const pointer IS legal.

If it weren't, this line would always result in a memory leak:
const int* i = new int;

Share this post


Link to post
Share on other sites
Quote:
C++ Standard, Final Draft, Section 5.3.5, Paragraph 2
If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object created by a new-expression, or a pointer to a subobject (1.7) representing a base class of such an object (10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete shall be the pointer value which resulted from a previous array new-expression.68) If not, the behavior is undefined. [Note: this means that the syntax of the delete-expression must match the type of the object allocated by new, not the syntax of the new-expression. ] [Note: a pointer to a const type can be the operand of a delete-expression; it is not necessary to cast away the constness (5.2.11) of the pointer expression before it is used as the operand of the delete-expression. ]

See the final note.

Σnigma

Share this post


Link to post
Share on other sites
const int* means that the value of the pointer can't change.
int const* means that what is being pointed to can't be changed but the value of the pointer can.

const int const* means that neither can be changed, this is completely const.

I believe this to be correct.

Dave

Share this post


Link to post
Share on other sites
Nearly.

const int * - pointer to an int which is const. The pointer can point at other things but whatever it's pointed at can't change.
int const * - same as above, pointer to an int which is const. The pointer can point at other things but whatever it's pointed at can't change.
int * const - const pointer to an int. The pointer can't point at anything else, what it points at can change.
const int * const - const pointer to an int which is const. Neither the pointer nor the object it points to can change.
int const * const - same as above, const pointer to an int which is const. Neither the pointer nor the object it points to can change.

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by iNsAn1tY
... What happens if delete is called on the returned pointer? Is the instance of the object it points to destroyed, thereby rendering the pointer held by the class which created it invalid?


delete destroys the object that the pointer points to. It does not affect the pointer itself or any other pointers that point to the destroyed object. boost::shared_ptr is a way to avoid accidental deletions, but it is not necessarily always the best way. When the lifetime of an object is well-defined, careful control of its construction and destruction is generally better than using boost::shared_ptr.

Share this post


Link to post
Share on other sites

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