Solved

std::string pointer

Posted on 2014-01-15
3
355 Views
Last Modified: 2014-01-15
std::string *str1;
int main (int argc, char* argv[]) {

        process();
        printf("String = %s\n", str1->c_str());  // THIS SUCCEEDED 

}

void process() {
        std::string str;
        std::set<uint64_t> vals;
        vals.insert(12);
        vals.insert(14);
        for(std::set<uint64_t>::iterator it = vals.begin(); it != vals.end(); ++it) {
                str += boost::lexical_cast<std::string>(*it);
                str += ":";
        }

        str1 = &(str);
}

Open in new window


Why does the printf in the main() does not seg faults and prints correct value 12:14:

Shouldn't it seg fault as it is trying to access memory which is out of scope. I mean the variable str inside process() function goes out of scope once the control returns from the function
0
Comment
Question by:perlperl
3 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
ID: 39782164
Hi perlperl,

IMO it depends on how the string class is implemented and what code the compiler creates. Of course it's errornous but this doesn't mean it has to crash in every case. If i.e. the string destructor doesn't reset some values (i.e. the pointer to the allocated memory) and if deallocating the memory in string destructor doesn't overwrite the content and there's coincidentally a terminating 0 char the printf can work without problems anyway.

BTW: I tested the same in Windows with Visual Studio, there it either crashs or outputs some trash.

ZOPPO
0
 

Author Comment

by:perlperl
ID: 39782249
I see.  I tried several times on my linux machine and it fails sometimes, so it all depends on if the memory was cleared during runtime or not.

Thanks
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39782426
This is an example of what the C++ standard would defined as "undefined behaviour". This means that anything can happen. It might work, it might not - it all depends on the platform and the compiler and which way the wind is blowing at the time. Sufficed to say, this type of code is always considered defective.

The standard also defines "unspecified behaviour", which is also platform and compiler dependent; however, this will always behave exactly the same for a given compiler and platform. This type of code is not considered defective; however, it is considered bad practice to rely on such code.

You can read more on my blog: http://evilrix.com/2013/05/09/doubt-and-uncertainty/
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now