|
From: <ud...@fa...> - 2003-12-07 17:34:52
|
Hi,
I am looking at this software for the first time...
I am writing an application and having a lot of memory
issues...
Can anyone help me with how I can approach this
problem?
This is one example..
Valgrind Output:
design_track>s
==24779== Invalid write of size 1
==24779== at 0x400202A7: strcpy
(mac_replace_strmem.c:174)
==24779== by 0x804EEAB: event_loop(void)
(TclMain.cxx:59)
==24779== by 0x804F340: main (TclMain.cxx:207)
==24779== by 0x42017498: __libc_start_main (in
/lib/i686/libc-2.2.5.so)
==24779== Address 0x430EB235 is 0 bytes after a
block of size 1 alloc'd
==24779== at 0x40027CB8: __builtin_vec_new
(vg_replace_malloc.c:203)
==24779== by 0x804EE88: event_loop(void)
(TclMain.cxx:58)
==24779== by 0x804F340: main (TclMain.cxx:207)
==24779== by 0x42017498: __libc_start_main (in
/lib/i686/libc-2.2.5.so)
==24779==
Code:
string complex_s="";
while( getline(std::cin, s) )
{
if( s.empty() ) // Ignore
empty lines
{
cout <<"design_track>";
}
else {
if(!complex_s.empty()) {
complex_s += "\n";
}
complex_s +=s;
if(s == "exit") {
break;
}
else
{
int length = s.length();
char *buf = new char[s.length()];
strcpy(buf,s.c_str());
|
|
From: Olly B. <ol...@su...> - 2003-12-07 17:42:56
|
On Sun, Dec 07, 2003 at 09:34:51AM -0800, ud...@fa... wrote:
> int length = s.length();
> char *buf = new char[s.length()];
> strcpy(buf,s.c_str());
The length of a C++ string doesn't include the terminating nul, so you
need to allocate an extra byte:
int length = s.length() + 1;
char *buf = new char[length];
strcpy(buf,s.c_str());
Cheers,
Olly
|