Skip navigation

Monthly Archives: May 2013

My first language was Java, and though I’ve been working with C++ for a bit now certain things still trip me up now and again.  While working on a class that tokenizes std::string objects based on std::string delimiters, I came across the use-case of wanting to modify char data returned from a string’s c_str() method.  Since this returns a const char* I tried

char* cstr = myStr.c_str();

but that throws a compiler error because it attempts the quite rightly illegal conversion between a const pointer and a mutable one.  In Java the idiom above re-implemented as below would be legal

final String testS = "final";
String testS2 = testS;
testS2 = "mutable";

This is legal in Java only because Java allocates new memory space for testS2 and copies the content of testS into it behind the scenes.  testS remains immutable while testS2 is mutable.  One simple way to obtain the same result in C/C++ is via strncpy() as follows:

std::string myString = "Hello"

const char* cstr_final = myString.c_str();

char cstr_mut[sizeof(cstr_final)];


With that cstr_mut is a fully modifiable clone of the content pointed to by cstr_final 🙂

side note: its obvious from my other posts that I enjoy including pictures– I have to advise against entering the search query “c string” in google image search while at work as it seems that there is an article of particularly risque lingerie by that name  *facepalm*


If you’ve ever tried to keep an instance of std::stringstream or another stream wrapper class as a member value in one of your own classes, you may have run across this set of friendly compiler errors:

“ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private.

iosfwd:55: error: within this context

iosfwd: In Copy Constructor ‘std::basic_stringstream…

streambuf:794: error” ‘std::basic_streambuf… is private

iosfwd:55: error: within this context

iosfwd: In Copy Constructor ‘std::basic_stringstream…”

This occurs because i/o streams cannot be copied!  You’d think they would bold that on every *stream page in the but sadly they don’t.  Anyway, if you want to work with streams you need to either pass around references/pointers to them or, preferably, create them on the stack as needed and let them pop off automatically.  More discussion available on StackOverflow here


As of at least ADT plugin version 21 there is a TID column displayed in Eclipse’s logcat viewer when you have display settings on maximum verbosity– this thread id is extremely important to parallel programming (or octopus oriented programming as I prefer to call it). However, the thread id will not be preserved if you simply copy and paste the content of this view from within Eclipse. Instead you’ll see something like [debug level] [timestamp] [invocation] (PID)… but no thread id. I don’t know if there is an easier way to grab the TID data directly from Eclipse, but it can be done using a remote adb shell as follows:

Execute the following from your PC’s command line: adb -s [target_device_serial_number] shell “logcat -v threadtime” > [target_output_file]

your session will appear to hang, but in actuality the device’s logcat output, exactly as shown in eclipse’s logcat view including the TID, is being written in real time to your output file. When you want to view the content, exit the logcat dump process with ctrl+c and open the file on your PC. Other options for logcat filtering etc. can be found here