your returning a stack allocated temporary array. That's undefined behavior, and your compiler is probably emitting a warning about that.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
There are several problems here: First, you cannot return a pointer to temporary memory -- the memory allocated for DataChar is invalidated as soon as Function returns, making it completely useless; second, while you can concatenate std::strings together, its more idiomatic in C++ to use stringsreams, and thirdly, with all of this combined together, you can use templates to make everything typesafe:
Well, kudos to you for trying. But it'll never work like that. floats and ints aren't stored as strings in memory, so you can't just convert them like that. Here are a couple popular options:
There are several problems here: First, you cannot return a pointer to temporary memory -- the memory allocated for DataChar is invalidated as soon as Function returns, making it completely useless; second, while you can concatenate std::strings together, its more idiomatic in C++ to use stringsreams, and thirdly, with all of this combined together, you can use templates to make everything typesafe:
and it breaks silently the first time you pass in a char* by accident. also, use reinterpret_cast, just to be more C++y
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
There are several problems here: First, you cannot return a pointer to temporary memory -- the memory allocated for DataChar is invalidated as soon as Function returns, making it completely useless; second, while you can concatenate std::strings together, its more idiomatic in C++ to use stringsreams, and thirdly, with all of this combined together, you can use templates to make everything typesafe:
Ok, I will simply assume that you do NOT want to convert your data into an actual string and have int x = 20 turn into char* = "20".
So first, your function is rather pointless and doesn't do anything that brute force casting your data to char* wouldn't do (without blowing up the stack with a ridiculously large array and pointless copying).
Second, even if this WOULD be working, you at no point seem to terminate that pseudo string you create with a 0. Trying to concatenate it with a string will just keep reading and copying until it hits the first zero that happens to be in memory. Best case: access violation to let you know there is a problem, worst case: seems to work most of the time and in debug builds, but suddenly sometimes breaks or copies garbage.
Third: please don't abuse the string class like that. If you want nothing but a generic buffer for data, use vector<uint8_t>. Anybody seeing "string" expects it to contain actual text, not raw binary data (also, most of the functions of string don't make any sense or will just break in a thousand ways).
The ugly way to do it quick and dirty would be something like:
Now this contains lots of ugly code, should also use a reinterpret_cast<char*>(&MyData), probably be split over 2 lines and it will still break if you use it on anything but native types (ie. do NOT try to use it on c-strings or arrays or stl containers). Yes, it could be used for structs, but if you try to use it to pass data to another program it can break (since you can't rely on it having the same layout in memory). Pragma pack should be looked at. Also byte order if using different platforms. It will also fail hard if your struct contains pointers or anything that requires more than a shallow copy.
While this might look a little cleaner, I would still consider it a case of "please only do this if you really know what you're doing": const char* rawPtr = reinterpret_cast<const char*>(&MyData); buffer.insert( buffer.end(), rawPtr, rawPtr + sizeof(MyData) );
You can use templates to hide it and save some work, but should be very careful to have specializations for some types (strings, pointers, arrays) and think REALLY hard about what happens if someone uses this on classes with virtual functions, containing stl containers, etc.
The real question might be: what exactly are you trying to do and what do you need that buffer for?
Here is an example that solves some of the problems with fastcall's code. Mainly it deals with the issue of char*, and also with pointer type problems (by using SFINAE). It also fixes the erroneous use of istream instead of ostream. #include <iostream> #include <string> #include <type_traits> #include <sstream> #include <cstdlib> template<class T> typename std::enable_if<std::is_fundamental<T>::value, std::ostream>::type& write_binary(std::ostream& s, T const& value) { s.write(reinterpret_cast<char const*>(&value), sizeof(T)); return s; } template<class CharType, class CharTraits> std::ostream& write_binary(std::ostream& s, std::basic_string<CharType, CharTraits> const& str) { s.write(reinterpret_cast<char const*>(&str.front()), str.length() * sizeof(CharType)); return s; } std::ostream& write_binary(std::ostream& s, std::string const& str) { s.write(reinterpret_cast<char const*>(&str.front()), str.length()); return s; } std::ostream& write_binary(std::ostream& s, std::wstring const& str) { s.write(reinterpret_cast<char const*>(&str.front()), str.length() * sizeof(std::wstring::value_type)); return s; } std::ostream& write_binary(std::ostream& s, char const* data, size_t length) { s.write(data, length); return s; } struct S { int a; char* b; }; int main() { std::ostringstream ss; write_binary(ss, 5); int mynumber = rand(); write_binary(ss, mynumber); write_binary(ss, "Hello world"); write_binary(ss, L"Hello world"); write_binary(ss, "Hello world", 11); // write_binary(ss, &mynumber); // fails, pointer to T is excluded by enable_if, thus no appropriate overload is found. // write_binary(ss, S); // fails, S is not a fundamental type. if an overload was available it would succeed though. for(auto val : ss.str()) { std::cout<<std::hex<<static_cast<int>(val)<<" "; } std::endl(std::cout); }
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
int WriteBinary(FILE* fp,uint64_t value) { return fwrite(&value,1,8,fp); }
int WriteBinary(FILE* fp,const char* value) { return fwrite(value,1,strlen(value),fp); }
int WriteBinary(FILE* fp,char* value) { return WriteBinary(fp,(const char*)value); }
int WriteBinary(FILE* fp,void* d,uint32_t l) { return fwrite(d,1,l,fp); }
int WriteBinary(FILE* fp,const string& value) { return WriteBinary(fp,(const char*)value.c_str()); }
int main() { FILE* fp = fopen("file","wb"); double d = 1234.56789; float f = 1234.1234f; int i = 1234; long long int l = 99999999; const char* s = "gggggg"; string ss = "ccccc"; const char* sss = "????";