Comments

: hi,: in C++ when a pointer is created with "new", it should have a corresponding "delete" when it has served its purpose, rite?: what about when the pointer is created as follows:: : int *myIntPointer;: : also, if I did this in C, would i need to do any "delete"ing?: : :

[blue]Pointers aren't created with "new", memory is allocated and typically the address of that allocated memory is assigned to an already existing pointer variable. The pointer variable itself is just like any other automatic variable... it has memory allocated on the stack that gets deallocated (popped of the stack) when the variable goes out of scope.

[code]{ // Start of new block of code as an example object * ptr; // This creates a pointer, not a call to "new" // Memory for variable "ptr" is pushed onto stack ptr = new object; // Here we allocate memory and assign the address // to "ptr"

... // Do some stuff

delete ptr; // Memory pointed to by "ptr" is deallocated, the // variable "ptr" itself still exists at this point} // Now variable "ptr" is popped of stack[/code]

: : hi,: : in C++ when a pointer is created with "new", it should have a corresponding "delete" when it has served its purpose, rite?: : what about when the pointer is created as follows:: : : : int *myIntPointer;: : : : also, if I did this in C, would i need to do any "delete"ing?: : : : : : : : [blue]Pointers aren't created with "new", memory is allocated and typically the address of that allocated memory is assigned to an already existing pointer variable. The pointer variable itself is just like any other automatic variable... it has memory allocated on the stack that gets deallocated (popped of the stack) when the variable goes out of scope.: : [code]: { // Start of new block of code as an example: object * ptr; // This creates a pointer, not a call to "new": // Memory for variable "ptr" is pushed onto stack: ptr = new object; // Here we allocate memory and assign the address: // to "ptr": : ... // Do some stuff: : delete ptr; // Memory pointed to by "ptr" is deallocated, the : // variable "ptr" itself still exists at this point: } // Now variable "ptr" is popped of stack: [/code]: : In C you would use malloc/free instead of new/delete.: [/blue]: [purple]in addition, u shud never try to deallocate memory which hasn't allocated by ur program, or already deallocated! as in ur example,

[black][b]int *myIntPointer;[/black][/b]

this declaration creates a pointer variable. what it is pointed to right now? myIntPointer has some garbage value (unless it is global). the memory it is pointed to right now is not allocated by ur program. since myIntPointer is a pointer,

[black][b]delete myIntPointer;[/black][/b]

will try to deallocate the contents of memory of that garbage location. this may crash the program.[/purple][hr][purple]~Donotalo()[/purple]

: : : hi,: : : in C++ when a pointer is created with "new", it should have a corresponding "delete" when it has served its purpose, rite?: : : what about when the pointer is created as follows:: : : : : : int *myIntPointer;: : : : : : also, if I did this in C, would i need to do any "delete"ing?: : : : : : : : : : : : : [blue]Pointers aren't created with "new", memory is allocated and typically the address of that allocated memory is assigned to an already existing pointer variable. The pointer variable itself is just like any other automatic variable... it has memory allocated on the stack that gets deallocated (popped of the stack) when the variable goes out of scope.: : : : [code]: : { // Start of new block of code as an example: : object * ptr; // This creates a pointer, not a call to "new": : // Memory for variable "ptr" is pushed onto stack: : ptr = new object; // Here we allocate memory and assign the address: : // to "ptr": : : : ... // Do some stuff: : : : delete ptr; // Memory pointed to by "ptr" is deallocated, the : : // variable "ptr" itself still exists at this point: : } // Now variable "ptr" is popped of stack: : [/code]: : : : In C you would use malloc/free instead of new/delete.: : [/blue]: : : [purple]: in addition, u shud never try to deallocate memory which hasn't allocated by ur program, or already deallocated! as in ur example,: : [black][b]int *myIntPointer;[/black][/b]: : this declaration creates a pointer variable. what it is pointed to right now? myIntPointer has some garbage value (unless it is global). the memory it is pointed to right now is not allocated by ur program. since myIntPointer is a pointer,: : [black][b]delete myIntPointer;[/black][/b]: : will try to deallocate the contents of memory of that garbage location. this may crash the program.: [/purple]: [hr][purple]~Donotalo()[/purple]: :

To prevent the problem of accidently deallocating memory to pointer which has not had memory allocated to it you should initialize your pointers to NULL, that is 0.

: : : : hi,: : : : in C++ when a pointer is created with "new", it should have a corresponding "delete" when it has served its purpose, rite?: : : : what about when the pointer is created as follows:: : : : : : : : int *myIntPointer;: : : : : : : : also, if I did this in C, would i need to do any "delete"ing?: : : : : : : : : : : : : : : : : : [blue]Pointers aren't created with "new", memory is allocated and typically the address of that allocated memory is assigned to an already existing pointer variable. The pointer variable itself is just like any other automatic variable... it has memory allocated on the stack that gets deallocated (popped of the stack) when the variable goes out of scope.: : : : : : [code]: : : { // Start of new block of code as an example: : : object * ptr; // This creates a pointer, not a call to "new": : : // Memory for variable "ptr" is pushed onto stack: : : ptr = new object; // Here we allocate memory and assign the address: : : // to "ptr": : : : : : ... // Do some stuff: : : : : : delete ptr; // Memory pointed to by "ptr" is deallocated, the : : : // variable "ptr" itself still exists at this point: : : } // Now variable "ptr" is popped of stack: : : [/code]: : : : : : In C you would use malloc/free instead of new/delete.: : : [/blue]: : : : : [purple]: : in addition, u shud never try to deallocate memory which hasn't allocated by ur program, or already deallocated! as in ur example,: : : : [black][b]int *myIntPointer;[/black][/b]: : : : this declaration creates a pointer variable. what it is pointed to right now? myIntPointer has some garbage value (unless it is global). the memory it is pointed to right now is not allocated by ur program. since myIntPointer is a pointer,: : : : [black][b]delete myIntPointer;[/black][/b]: : : : will try to deallocate the contents of memory of that garbage location. this may crash the program.: : [/purple]: : [hr][purple]~Donotalo()[/purple]: : : : : : To prevent the problem of accidently deallocating memory to pointer which has not had memory allocated to it you should initialize your pointers to NULL, that is 0.

I disagree. It is better to solve your bugs than assuming that you will write bugs later on and therefore take precautions. If your program starts deleting memory in the wrong places, it is a serious bug which you will only hide away by initializing the pointer to NULL.

And if we are talking C, you don't know that NULL == 0. It may be zero or it might be a typecasted null pointer like: