Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Automatic memory deallocation by std::string ??

Reply
Thread Tools

Automatic memory deallocation by std::string ??

 
 
qazmlp
Guest
Posts: n/a
 
      03-07-2004
Is there a memory leak here? Can't the memory deallocation happen
automatically as the std::string object anyway is owning the memory?
#include <cstring>
#include <string>
#include <iostream>

char* toString()
{
const char* cStr = "cStr" ;
char* ptr = new char[ std::strlen( cStr ) + 1 ] ;
std::strcpy( ptr , cStr ) ;

return ptr ;
}

int main()
{
std::string cppStr( toString() ) ;
std::cout<< cppStr << std::endl;
}
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-07-2004

"qazmlp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Is there a memory leak here?


Yes

> Can't the memory deallocation happen
> automatically as the std::string object anyway is owning the memory?


But that's the point, std::string does not own the memory. It always
allocates its own memory.

> #include <cstring>
> #include <string>
> #include <iostream>
>
> char* toString()
> {
> const char* cStr = "cStr" ;
> char* ptr = new char[ std::strlen( cStr ) + 1 ] ;
> std::strcpy( ptr , cStr ) ;
>
> return ptr ;
> }
>
> int main()
> {
> std::string cppStr( toString() ) ;
> std::cout<< cppStr << std::endl;
> }


john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3D vector memory deallocation madhu C++ 6 11-13-2006 07:02 AM
memory allocation deallocation ... pasa_1 C++ 19 11-02-2006 07:56 PM
std.textio, readline and memory deallocation Nicolas Matringe VHDL 9 09-04-2006 12:15 PM
memory allocation/deallocation question BigBrian C++ 12 04-07-2005 12:32 AM
Stack Memory Deallocation Problem yccheok@gmail.com C++ 2 12-18-2004 02:35 PM



Advertisments