Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
combine c++ string with dynamically allocated c array of chars through overloaded add operator
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 [v] //clang 3.8.0 [v] //g++ 5.4.0 [v] //combine c++ string with dynamically allocated c array of chars through overloaded add operator // C++ headers #include <iostream> #include <string> // C headers #include <stdlib.h> #include <string.h> /** test function simulating data read * (fills the buffer "*pdata" with something * and the buffer "*dataSize" with its size */ void getData(char** pdata, int* dataSize); /** simply releases the data get with "getData()" */ void freeData(char** pdata); int main() { std::string s("my text is: "); // C++ std::string std::string t; // result char* data; // array of chars int siz; // the size of the array of chars // getData(&data, &siz); t = s + "\"" + data + "\""; std::cout<<t<<std::endl; // print result freeData(&data); // free resource(s) // return 0; } void getData(char** pdata, int* dataSize) { const char sz[] = "the quick brown fox"; *dataSize = strlen(sz); *pdata = (char*)malloc(*dataSize); memset(*pdata, 0, *dataSize); memcpy(*pdata, sz, *dataSize); } void freeData(char** pdata) { free(*pdata); }
run
|
edit
|
history
|
help
0
Compare boost optional with the actual value
ExceptionHandling4
4C test
Cotton Farm 0.0.1.0
ContainerVector
typecasting
PhoneDirectory
TREE - path from root to leaf with given sum
TempQuickDoubly
Merge problem