|
-
December 22nd, 2012, 02:44 AM
#7
Re: Validity of new/delete pair (or malloc/free) after pointer casting
I see; thanks very much for your input. So, if I were reading data from a file using an ifstream (which requires a char* to fill with data) in someFunction(...), it would be better to do:
Code:
void someFunction(float* &result)
{
//Assume some open ifstream called "in" and a known integer, fileSize
int numFloats = fileSize / sizeof(float);
result = new float[numFloats];
in.read((char*)result, numFloats * sizeof(float));
}
Thereby avoiding the cast after allocation entirely, per laserlight. Then, I can just call delete[] floatData in the main subroutine to avoid any potentially undefined behavior?
Understood correctly?
Best Regards,
BioPhysEngr
http://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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|