I have the following code,
char* MyClass::MyMethod()
That line is a syntax error. The only place that a colon (':')
can occur in a type description in C is as a bit-field length
within a structure definition.
{
char* ptr = new char[6]; // space for NULL terminator
That line is a syntax error. There is no storage class named 'new'
in C.
ptr = "hello";
return ptr;
delete[] ptr;
Another syntax error.
}
What I'm trying to do is return ptr, but delete[] it afterwards. In the
code above, I believe the delete[] line will not be executed following
the return statement.
How should I achieve this?
How can you be assured that the pointer would not be deleted until
after the calling routine had finished accessing the data??
If you create storage and return a pointer to that storage, you cannot
also delete that storage, not unless the calling routine will never
attempt to access the storage nor to test the pointer against anything
other than a null pointer constant.
One would suspect that your code is not in fact C code, in
which case our most charitable assumption would be that you
have -accidently- posted your question to the wrong newsgroup.
But even if so, you need to figure out what it is supposed to
mean to delete a pointer to a storage area when you have returned
that pointer outwards. There are not many languages around
(-none- that I can think of right now) in which returning a pointer
to a memory block is understood to mean that the procedure
calling mechanism should take a -copy- of the storage pointed to
and put that copy somewhere for future use.