I am implimenting an IO function that will write numeric types (int,
long, double, etc) in binary format. I am casting them to a char* in
order to implement byte-swapping from big-endian to little-endian
before writing them out. To that end, I am using re-interperet-cast
and assigning the result to a char *. I tried to clean up the code
with a delete statement, but this seems to cause an exception.
My question is, will the memory assigned to buf be reclaimed when the
function goes out of scope? If I pass a long, buf should be pointing
to an array of size 4 - why does the delete statement cause a fault?
template <class T>
void put(T& t, std::ostream& s)
{
size_t size = sizeof(T);
char* buf = reinterpret_cast<char *>(&t); // Isn't buf pointing to a
char array?
//byteswap buf
s.write(buf, size);
delete [] buf; //This causes an exception - why?
}
Thanks for your help.
__tryptik