If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?

Thanks for any guidance you might have.

Last edited by BioPhysEngr; December 22nd, 2012 at 12:50 AM.
Reason: Ah, my original code will segfault. Corrected syntax to pass the result pointer by referece.

Best Regards,

BioPhysEngrhttp://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Pretty much a simple question, but I wanted to confirm what I think I understood from reading elsewhere:

Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.

Why not just declare the data as char from the beginning, do whatever you want with it, and then delete it? Then you need no casting. Or do the opposite -- declare a float*, allocate as a float*, and delete. No casting needed.

Also, what if there is an issue at the end of the buffer? For example can you guarantee that 1000/sizeof(float) is evenly divisible? That last float at the end would tread on unallocated memory.

But I feel that the behaviour is technically undefined. Casting the delete to some other type is asking for trouble in the long run. The thing saving you is that the types are simple types (char, float), but if it were a non-POD type, this wouldn't work out too well. Also, if for some reason array delete is overloaded, then all bets are off, even with simple types.

I would be surprised if it was. The delete[] operator takes a void* parameter so regardless of whether you pass a char* or a float* that type information is lost in the call anyway.

But although correct this type of casting isn't directly elegant. I would separate the external and internal representation of the numbers. On file I would use a text format and then convert to and from the internal binary format. Boost has a conversion library called lexical_cast one can use for this.

That's not entirely correct. When you use delete [], the destructors for each individual object are called first. If you replace char and float by car and bike classes, it's obvious that using the wrong type when calling delete [] is asking for trouble. Because intrinsic types don't have destructors, this is not really a problem here, but it could be in the future if the types are changed to something else, and you're not aware that of the difference in types in new [] and delete [].

Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.

probably nuzzle is confusing a delete expression with a call to operator delete. In the former case no cast is performed for pointer types ( conversion operators to pointer will be invoked for non pointer types though; BTW, invoking delete on a void* is an error in c++11 because now pointer operands are requried to be pointers to object type; in c++2003 it was just UB ).