How does std::string::c_str() return a c-string that does not cause a memory leak or undefined c-string contents?



I'm writing a string class that is similar to std::string for a homework assignment, but I cannot figure out how to return a c-string that does not cause a memory leak and is guaranteed to stay the same until it is no longer in use. I currently have:


const char* string::c_str()
{
char c[_size+1];
strncpy(c,_data,_size);
c[_size]='';
return c;
}

but the contents are overridden shortly after it is called. If I do dynamic allocation, I'll have either a memory leak or only one c-string can exist from a given string at any time. How can I avoid this?


Related to : How does std::string::c_str() return a c-string that does not cause a memory leak or undefined c-string contents?
How does std::string::c_str() return a c-string that does not cause a memory leak or undefined c-string contents?
Programming Languages

I'm writing a string class that is similar to std::string for a homework assignment, but I cannot figure out how to return a c-string that does not cause a memory leak and is guaranteed to stay the same until it is no longer in use. I currently have:


const char* string::c_str()
{
char c[_size+1];
strncpy(c,_data,_size);
c[_size]='';
return c;
}

but the contents are overridden shortly after it is called. If I do dynamic allocation, I'll have either a memory leak or only one c-string can exist from a given string at any time. How can I avoid this?


Process.GetProcessesByName(String, String) Memory Leak
Programming Languages

I have a piece of code that gets a list of processes on a remote computer using the static method Process.GetProcessesByName(String, String), this runs on a lot of computers (a few thousands) and I've noticed it's a cause of a major memory leak.


I ran ANTS memory profiler which told me that most of my memory is taken by strings, strings containing strage values like "% Idle Time", "Processor Information", and "Cache Faults/sec". I've recognized those strings as probably being a part of Performance Counters in the program, the problem is I don't have any performance counters in the program.


Digging deeper found out those strings are held in hashtable

In C++ does returning a string created from a local char array cause a memory leak or undefined behavior?
Programming Languages

I am wondering if this would cause a memory leak or an undefined outcome in C++?


string foo()
{
char tempArray[30];
strcpy(tempArray, "This is a test");
return string(tempArray);
}

I know this is a bad thing in C but I haven't found a definite answer for C++.


So everyone is saying no, but I am still confused as to when the memory is deallocated?


Lets say I have this method that calls the above method


void bar()
{
string testString = foo();
}

At what point in the above code does the

Memory leak with std::string when using std::list<std::string>?
Programming Languages

I'm working with std::list<std::string> in my current project. But there is a memory leak somewhere connected with this. So I've tested the problematic code separately:


#include <iostream>
#include <string>
#include <list>
class Line {
public:
Line();
~Line();
std::string* mString;
};
Line::Line() {
mString = new std::string("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
}
Line::~Line() {
//mString->clear(); // should not be neccessary
delete mString;
}
int main(int argc, char** argv)


HD Wallpapers
3D
3D Abstract
City
Celebrities
Indian Celebrities
Cars
Travel
Girls
Animals Birds
Movies
Sports
Black White
Nature
Planes
Anime
Food Drink
Dreamy Fantasy
Architecture
Games
Space
Holidays
Flowers
Love
Artistic
Baby
Beach
Bikes Motorcycles
Macro
Computers
Vector
Funny
Army
Textures
Brands
Misc
Music
Other
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .