472,981 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

is there memory corruption here?

I've been told there is memory corruption for buf.str().c_str() Can
someone explain why?:

void myFunc()
{
std::stringstream buf;
buf << "some string";
const char *data = buf.str().c_str();

SomeFunc(data, strlen(data));
//do something else

}
Sep 11 '08 #1
2 2069
Bob Doe wrote:
I've been told there is memory corruption for buf.str().c_str() Can
someone explain why?:

void myFunc()
{
std::stringstream buf;
buf << "some string";
const char *data = buf.str().c_str();
str() returns a string by value, meaning you are calling c_str() on a
temporary string value that is destroyed after this line. The array that
the pointer returned by c_str() points to is only valid as long as the
string does exist (and isn't modified). So after that line, data is a
dangling pointer that you must not dereference anymore.
SomeFunc(data, strlen(data));
//do something else

}

Sep 11 '08 #2
Bob Doe 写道:
I've been told there is memory corruption for buf.str().c_str() Can
someone explain why?:

void myFunc()
{
std::stringstream buf;
buf << "some string";
const char *data = buf.str().c_str();

SomeFunc(data, strlen(data));
//do something else

}
Why you use stringstream and const char * together?

the std::stringstream::str() return an temp std::string object, which is destroyed after that line.
you'd better do it like this:

std::string data;
std::stringstream buf;

buf << "some string";
buf << some_int;
data = buf.str();

SomeFunc(data.c_str(),data.length());
Sep 11 '08 #3

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

Similar topics

5
by: Noa Garnett | last post by:
I'm developing on C++, using visual studio 6.0 with service pack 5. I have a memory corruption while debugging. Some of the variables I'm using are suddenly set to zero while progressing along the...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
4
by: indushekara | last post by:
Hi, We are having memory corruption in our application somewhere, unable to find out. one part of code we found that we are specifying wrong format specifier. Could anyone let me know if the...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
4
by: Harsha | last post by:
Hi All, There is some memory corruption in my application. It is due to the fact that it is multithreaded. Is this due to some missing mutex locking? Is there any way to find where it is...
4
by: T Clancey | last post by:
Hi, I have an app which connects to an Access database on a server, there are around 10 clients. It's a fairly heavy data intensive app, but most of the traffic is look up data. While the...
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
2
by: Fredo | last post by:
First of all, I apologize for cross-posting. I posted this on the framework.interop group as well, but haven't received a response yet, so I thought I'd try here... I'm trying to write an app...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.