return statement and delete

A

A

Hi,

consider this:


char* aClass::printHello()
{
char* ptr = new char[6];
ptr = "Hello";
return ptr;
delete[] ptr;
}

Is it possible to return ptr and then delete[] ptr afterwards. I think the
delete[] ptr statement will not be executed after the return statement. Any
workaround for this?


REgards
ML
 
J

Josephine Schafer

A said:
Hi,

consider this:


char* aClass::printHello()
{
char* ptr = new char[6];
ptr = "Hello";
return ptr;
delete[] ptr;
}

Is it possible to return ptr and then delete[] ptr afterwards. I think the
delete[] ptr statement will not be executed after the return statement.

That's correct.
Any workaround for this?
call delete from the function that called printHello.

As a library provider it is not always desirable to write such a code since
client code then needs to remember to delete the pointer returned.

HTH,
J.Schafer
 
G

Gregg

A said:
Hi,

consider this:


char* aClass::printHello()
{
char* ptr = new char[6];
ptr = "Hello";
return ptr;
delete[] ptr;
}

Is it possible to return ptr and then delete[] ptr afterwards. I think the
delete[] ptr statement will not be executed after the return statement. Any
workaround for this?


REgards
ML

I'm not sure what you want to do, but I think you misunderstand what pointer
assignment does. First you are allocating memory and setting ptr to point to
it

char* ptr = new char[6];

and then you are setting ptr to point to something else

ptr = "Hello";

after which the originally allocated memory is no longer pointed to by
anything, so you can't delete the memory and it is leaked.

Aside from this, if you want to automatically delete memory upon returning
from a function, use either std::vector (for arrays) or std::auto_ptr (for
single object).
 
J

Josephine Schafer

Josephine Schafer said:
A said:
Hi,

consider this:


char* aClass::printHello()
{
char* ptr = new char[6];
ptr = "Hello";
return ptr;
delete[] ptr;
}
Oops..You are doing a memory leak for sure.
ptr first points to a valid dynamic store address (1st line) and then you change
it to
point to a string literal in your second stmt (2nd line).
Now calling delete on a pointer that doesn't point to a valid dynamic store
address is not correct.
 
D

David White

A said:
Hi,

consider this:


char* aClass::printHello()

The function doesn't print anything.
{
char* ptr = new char[6];
ptr = "Hello";
return ptr;
delete[] ptr;
}

Is it possible to return ptr and then delete[] ptr afterwards.

'return' means the function returns, so any delete after that cannot be done
by the function.
I think the
delete[] ptr statement will not be executed after the return statement. Any
workaround for this?

You have to decide whether you want the delete to be before or after the
function returns. If before, you should not return a pointer to deleted
memory. If after, the delete cannot be done by the function.

What exactly are you trying to do? Maybe this would do what you want:

std::string aClass::getHello() const
{
return "Hello";
}

DW
 
D

David White

David White said:
The function doesn't print anything.
{
char* ptr = new char[6];
ptr = "Hello";

At least someone (Gregg, at my newsreader so far) noticed the biggest
problem. It looks as though it was _intended_ that "Hello" be copied to the
allocated buffer, even though I missed that this isn't what the code
actually does.

DW
 
J

jeffc

A said:
char* aClass::printHello()
{
char* ptr = new char[6];
ptr = "Hello";
return ptr;
delete[] ptr;
}

Is it possible to return ptr and then delete[] ptr afterwards. I think the
delete[] ptr statement will not be executed after the return statement.

Of course not.
Any workaround for this?

Well, the "workaround" is obviously to delete the ptr before the function
call. But obviously, that will delete the ptr before you can use what it's
pointing to. If you are going to write code like this, then you have to
think about the responsibility for the memory. As written, the responsible
party has to be the caller, not the function itself. i.e. the caller would
be responsible for deleting the storage.
 
J

jeffc

jeffc said:
Well, the "workaround" is obviously to delete the ptr before the function
call. But obviously, that will delete the ptr before you can use what it's
pointing to. If you are going to write code like this, then you have to
think about the responsibility for the memory. As written, the responsible
party has to be the caller, not the function itself. i.e. the caller would
be responsible for deleting the storage.

Taking into account the error that Josephine pointed out, too :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top