Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Access to temporary object
//clang 3.8.0 #include <iostream> #include <cstring> struct A { A(const char* data) { strcpy(this->data, data); std::cout << "A constructed" << std::endl; } A(const A& a) { strcpy(this->data, a.data); std::cout << "A copy-constructed" << std::endl; } ~A() { memset(data, '\0', sizeof(data)); std::cout << "A destroyed " << std::endl; } const char* c_str() { return data; } // return address of member private: char data[100]; // some member }; A return_A_by_value(const char* data) { A a(data); // create A on stack const char* memberData = a.c_str(); std::cout << "Here we can refer to objects's data since 'a' exists at this time: " << memberData << std::endl; return a; // temporary object is returned } int main() { const char* data = return_A_by_value("[some text]").c_str(); // at the end of this line A will be destroyed try { // Dereferencing data can lead to undefined behavior // since it is pointed to data member of destroyed object std::cout << "Here data is not valid pointer: <" << data << ">" << std::endl; } catch(const std::exception& e) { std::cout << e.what() << std::endl; } }
run
|
edit
|
history
|
help
0
Assertion Divide by Zero Example
Throttle Example using a circular queue (Push all but 2 less than maxSize; then pop all but 2 of current size)
Namespace scope qualifier
Calculate H.C.F using recursion
ghfhfgh
non-deduced context
marquee text in C++
11/18
Non type template argument
test string