Advertisement

simple (i hope) c++ question

Started by November 18, 2000 01:26 AM
2 comments, last by [bobafet] 24 years, 1 month ago
Okay, let''s say you have this code: int *test; test = new int; void *bleh = test; now.. let''s say we want to free the memory allocated by the new operator.. would this work? : delete bleh; or would i have to do something like this? : delete ((int *)bleh); any help with this is greatly appreciated - Ian Perez (fett@willcomp.com) - "It is by will alone I set my mind in motion"
- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"
why not test it?
(OK, delete must now the size)
Advertisement
it depends on just how strongly typed your c++ implementation is.

vc++ and borland builder i believe would require you to cast it, and would yell at you if u casted it using c-style casting (as oppossed to say dynamic_cast<>() or reinterpret_cast<>())

borland 5.02 might not, but that''s cuz it was before ansi/iso finally came ''round, i think

why u gotta do that, anywhoo?
if you have to code that way, then i would just cast it anyway, to be safe

just my 2 cents

//////////////////////////////

I have no name that you may call me. I am Succinct, so call me as such.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
-- Succinct(Don't listen to me)
I''m not sure with a void pointer if you have to cast. I believe any pointer can be assigned to a void pointer as well as any numeric value without warnings or errors. When you free it there shouldn''t be any problem. I believe when you allocate memory and additional four bytes are allocated to store the length. So what you free is (bleh - 4) for a length of (int)*(bleh - 4). When I tested with C++ Builder the length was a minimum of 14, but otherwise the size needed + 6. The real problem is on classes where the correct destructor will not be called so if the class allocated any additional data that data will not be freed by the destructor as it should have. The space for the class itself will be freed properly though.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement