[RESOLVED] Validity of new/delete pair (or malloc/free) after pointer casting
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.
My code looks like:
void someFunction(float* &result)
char * tmp = new char;
//...Fill the char buffer here...
result = (float*)tmp; //Reinterpret binary data as floats
float * floatData;
delete (char*)floatData; //Release the memory
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.
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.