Skip to content

Instantly share code, notes, and snippets.

@hiepnd
Last active August 29, 2015 13:56
Show Gist options
  • Save hiepnd/9303658 to your computer and use it in GitHub Desktop.
Save hiepnd/9303658 to your computer and use it in GitHub Desktop.
Never trust a C++ function that return char *
#include <iostream>
/**
* You can run online at http://code.hackerearth.com/
*/
using namespace std;
const char *doubleString(const char *s){
string ss = string(s) + string(s);
return ss.c_str();
}
int main() {
const char *hello = "hello";
const char *hellox2 = doubleString(hello);
cout << "It should be \"" << hello << hello << "\"" << " actual: " << hellox2 << endl;
doubleString("dummy");
cout << "It should be \"" << hello << hello << "\"" << " actual: " << hellox2 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment