If I delete a pointer, does it delete the data in the current address or all of it?
example:
bool *dat;
dat=new bool;
dat=true;
++dat;
dat=false;
delete dat;
--dat;
if I printed dat, would it return true or just a random address?
If I delete a pointer, does it delete the data in the current address or all of it?
example:
bool *dat;
dat=new bool;
dat=true;
++dat;
dat=false;
delete dat;
--dat;
if I printed dat, would it return true or just a random address?
What you are doing is very dangerous!
bool *dat; //create a bool pointer
dat=new bool; //set bool pointing to a address, i.e pointer-to-bool
dat=true; //give value to the pointed address
++dat; //increase the address of bool. Huh-oh, but where is the
//next address. Even worse, what does that address
//contains? Important data ? maybe
dat=false; //what ever it had now its set to false
delete dat; //now you are deleting a vairable that points to a
//different that what it starts with!
--dat; //Its deleted already, so huh?
ah, that makes things much more clear; I guess I'll just stacks in that case. Thanks
"If I delete a pointer, does it delete the data in the current address or
all of it?"
If you delete a pointer that variable no longer exist so every thing it
contains also gets deleted.
"If I delete a pointer, does it delete the data in the current address or
all of it?"If you delete a pointer that variable no longer exist so every thing it
contains also gets deleted.
delete operator releases (deallocates) a block of memory
. The cast-expression argument must be a pointer to a block of memory previously allocated for an object created with the new operator. Using delete on a pointer to an object not allocated with new gives unpredictable results.
The new keyword allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object. If unsuccessful, by new returns zero or throws an exception;
The delete operator deallocates a block of memory; which frees memory back to the available pool.
Links -
http://www.cplusplus.com/reference/std/new/
Understanding operating systems By Ida M. Flynn, Ann McIver McHoes
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.