c++ - Converting a std string to char* using const cast -



c++ - Converting a std string to char* using const cast -

after days of attempting create shell inquire bit of help. have started on 4 or times different info structures , plea solution below problem. have string need break individual arguments , have pointer that. pass args exec function since cant seem fill args correctly getting funny results, here simplified version of whats happening

char* args[100]; int counter=0; string temp = "some text , stuff here"; stringstream s (temp); while(s>> temp) { cout << "token " << counter << " =" << temp <<endl; args[counter]=const_cast<char *> (temp.c_str()); counter++; } //print debug info for( int ii=0; args[ii] != null; ii++ ) { cout << "argument out " << ii << ": " << args[ii] << endl; }

this code doesn't work , cant grasp why. result stores "here" in every value of args counter gets changed.

token 0 =some token 1 =text token 2 =and token 3 =stuff token 4 =here argument out 0: here argument out 1: here argument out 2: here argument out 3: here argument out 4: here

probably because temp object re-using internal allocation. when store c_str() result storing memory address. std::string class not create new allocation each time read string stream, rather reuses allocation has (if possible).

further, using pointer returned c_str() after have done anything else std::string object obtained invokes undefined behavior.1

if possible, alter args std::vector<std::string>. if not possible need strdup() pointer returned c_str() in order create exclusively new allocation copies value of string @ moment. must, of course, remember free() allocations when done.

additionally, casting away const qualifier , writing pointer results in undefined behavior.2 @ minimum need alter args const char * args[100];, suggest using vector of strings instead.

1 http://en.cppreference.com/w/cpp/string/basic_string/c_str

the pointer obtained c_str() may invalidated by:

passing non-const reference string standard library function, or calling non-const fellow member functions on string, excluding operator[], at(), front(), back(), begin(), rbegin(), end() , rend().

2 http://en.cppreference.com/w/cpp/string/basic_string/c_str

writing character array accessed through c_str() undefined behavior.

based on comment indicating need utilize exec(), sounds need array of pointers-to-char. however, can still using vectors. need 1 vector hold std::string objects, own char* allocations. can utilize vector hold actual pointers. this:

const char * binarypath = "/bin/foo"; std::vector<std::string> argstrings; std::vector<char *> argpointers; std::string temp = "some text , stuff here"; istringstream s(temp); // argv[0] should contain binary's path. argpointers.push_back(binarypath); while (s >> temp) { argstrings.push_back(temp); std::cout << "token " << argstrings.size() << " =" << argstrings.back() << std::endl; // cast away const required exec() family of functions. argpointers.push_back(const_cast<char *>(argstrings.back().c_str())); } // exec() functions expect null pointer terminate arguments array. argpointers.push_back(nullptr); // can our exec safely. execv(binarypath, &argpointers[0]);

in case argstrings owns actual string allocations, , using argpointers hold array of pointers passing execv(). const_cast safe because execv() not modify strings. (the argument char * const [] compatibility older c code; functions behave though argument const char * const [].)

c++ string const-cast

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -