An allocated variable returning by a fuction is unallocated automatically ?

O

orion30

I would like to know, if an allocated variable returning by a fuction is
unallocated automatically ?

If no how to proceed ?

Best regards

------------------------------------------------------------------------

In my case, I want to return aa. After the call of polit, is aa is
unallocated automatically ?

char * polit(char *expressions)
{
..
..
..
aa = (char *)malloc(strlen(Result)*sizeof(char)+1);
..
..
..
return(aa);
}
 
A

Alf P. Steinbach

I would like to know, if an allocated variable returning by a fuction is
unallocated automatically ?
No.



If no how to proceed ?

Use smart-pointers. Use std::auto_ptr for simple
transfer of ownership (deallocation responsibility).
Use boost::shared_ptr for a pointer that isn't
necessarily "owned" by a single something.

std::auto_ptr lives in standard header <memory>.

boost::shared_ptr: go to [http://www.boost.org].


In my case, I want to return aa. After the call of polit, is aa is
unallocated automatically ?

char * polit(char *expressions)
{
.
.
.
aa = (char *)malloc(strlen(Result)*sizeof(char)+1);

Should be

aa = new char[ strlen(Result) + 1];

in C++.
 
A

Alf P. Steinbach

0On said:
I would like to know, if an allocated variable returning by a fuction is
unallocated automatically ?
No.



If no how to proceed ?

Use smart-pointers. Use std::auto_ptr for simple
transfer of ownership (deallocation responsibility).
Use boost::shared_ptr for a pointer that isn't
necessarily "owned" by a single something.

std::auto_ptr lives in standard header <memory>.

boost::shared_ptr: go to [http://www.boost.org].


In my case, I want to return aa. After the call of polit, is aa is
unallocated automatically ?

char * polit(char *expressions)
{
.
.
.
aa = (char *)malloc(strlen(Result)*sizeof(char)+1);

Should be

aa = new char[ strlen(Result) + 1];

in C++.

return(aa);
}


Sorry, I didn't think. You're probably trying to achieve something
else than the code indicates. In that case, use std::string.
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top