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?

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)
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
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
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
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?

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...

Programming Languages

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow