wierd output using const char*, to_string() and c_str()

I had a problem in a bigger program and I made another smaller program to show the problem. I was expecting the output to be

1 10 10 10 *************** 2 10 10 10 **************** 3 10 10 10 *****************


However the result was different. Where did I make the mistake? Is it because it is const char * type? I am really confused. Thank you.

#include<iostream> #include<string> using namespace std; int main() { int order=1; for(int i=1; i<10;++i,++order){ const char* a[2]; int b = 10; a[0] = to_string(order).c_str(); a[1] = to_string(b).c_str(); cout << a[0] << endl; cout << a[1] << endl; cout << "**************" << endl; } }


10 10 ************** 10 10 ************** 10 10 ************** 10 10 ************** 10 10 ************** 10 10 ************** 10 10 ************** 10 10 ************** 10 10 **************


In this line

a[0] = to_string(order).c_str();

to_string(order) generates a temporary string. The result of c_str() is only valid as long as the temporary string exists, but the temporary string is destroyed after the statement. a[0] is then left holding an invalid pointer.

To work around this, you'll need to store the strings somewhere so they don't go away immediately:

#include<iostream> #include<string> using namespace std; int main() { int order=1; for(int i=1; i<10;++i,++order){ const char* a[2]; int b = 10; auto order_str = to_string(order); auto b_str = to_string(b); a[0] = order_str.c_str(); a[1] = b_str.c_str(); cout << a[0] << endl; cout << a[1] << endl; cout << "**************" << endl; } }


  • C++ String tokenisation from 3D .obj files
  • How to add System.Windows dll to Visual Studio 2010 express?
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • WPF Template Binding in ToggleButton UserControl
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • How does this usort cmp function actually work?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Breeze - Deleted Items nav properties bug
  • Meteor helpers not available in Angular template
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • javaw.exe and eclipse startup problems
  • output of program is not same as passed argument
  • MySQL WHERE-condition in procedure ignored
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • need help with bizarre java.net.HttpURLConnection behavior
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal