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?
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)
Contents changed(cleared?) when access the pointer returned by std::string::c_str()
Programming Languages

I have to maintain some legacy codes,parts of it look like the following:


/* this class reads a ini file which looks like this:
key = value
*/
class Config{
// ..
public:
string conf(const char* key)
{
vector<string> v;
//..
v = func(); //this function returns a vector<string>
return v[1];
}
};
void test()
{
// a globla Config object cfg is initialized outside
const char* p = cfg.conf("key1").c_str();
// the string object will be alive as a auto var

C++ std::string memory leak
Programming Languages

I've got this class AppController and the function connectPlayer:


/* AppController.h */
class AppController
{
// Some other declarations ...
private:
static const string TAG;
};
/* AppController.cpp */
#include "AppController.h"
const string AppController::TAG = "AppController";
AppController::AppController() {
/* some code here...*/
}
void AppController::connectPlayer() {
std::string port;
std::string host;
port = CM->getMenu()->getData("PORT");
h
Can make STL string::c_str() return NULL when it has no string?
Programming Languages

My project has legacy library which consider NULL pointer as empty string.


But when I get return data from std::wstring like this,


std::wstring strData;
const wchar* pStr = strData.c_str();
ASSERT(NULL == pStr); // ASSERT!!

pStr is not NULL but pointer which wstring point.


Can I make std::string return NULL when it has no string data?
Now I wrap every str member variable like this :


const wchar* GetName() { // I hate this kinds of wrapping function
if (m_Name.empty())
{
return NULL;
}
return m_Name.c
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?


What's the difference between std::string::c_str and std::string::data?
Programming Languages

Why would I ever want to call std::string::data() over std::string::c_str()? Surely there is some method to the standard's madness here...



Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .