473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Automatic memory deallocation by std::string ??

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;
}
Jul 22 '05 #1
1 3065

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c 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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sergey Tursanov | last post by:
Hello all ! When I use code below and check my program with various memory leak detection tools I get message about memory leaks in stl_alloc.h. #include <string> int main() { std::string s...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
1
by: Axel | last post by:
Hiho, how can I reserve enough memory for a long string? I tried with max_size() but 4GB is to much for my system, an exception ist the result. And I'm getting an exception too when using the...
9
by: Bryan Bullard | last post by:
#include <cstring> #include <iostream> #include <string> std::string foo() { char buf; strcpy(buf, "this is a test.");
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
1
by: gabriel.becedillas | last post by:
I have a lot of functions returning "const std::string&". Every time I wrap one of those I have to do it like this: class_.def("name", &someclass::bla,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.