C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
deleting pointers Posted by digitalrana on 6 Sept 2006 at 5:15 AM
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?


Report
Re: deleting pointers Posted by HK_MP5KPDW on 6 Sept 2006 at 5:37 AM
: 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?
:
:
:

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.

{                      // 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


In C you would use malloc/free instead of new/delete.

Report
Re: deleting pointers Posted by Donotalo on 6 Sept 2006 at 5:52 AM
: : 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?
: :
: :
: :
:
: 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.
:
:
: {                      // 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
: 

:
: In C you would use malloc/free instead of new/delete.
:

:

in addition, u shud never try to deallocate memory which hasn't allocated by ur program, or already deallocated! as in ur example,

int *myIntPointer;[/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,

delete myIntPointer;[/b]

will try to deallocate the contents of memory of that garbage location. this may crash the program.


~Donotalo()

Report
Re: deleting pointers Posted by bluj91 on 7 Sept 2006 at 7:03 PM
: : : 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?
: : :
: : :
: : :
: :
: : 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.
: :
: :
: : {                      // 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
: : 

: :
: : In C you would use malloc/free instead of new/delete.
: :

: :
:
: in addition, u shud never try to deallocate memory which hasn't allocated by ur program, or already deallocated! as in ur example,
:
: int *myIntPointer;[/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,
:
: delete myIntPointer;[/b]
:
: will try to deallocate the contents of memory of that garbage location. this may crash the program.
:

:
~Donotalo()
:
:

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.
Report
Re: deleting pointers Posted by Lundin on 7 Sept 2006 at 11:26 PM
: : : : 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?
: : : :
: : : :
: : : :
: : :
: : : 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.
: : :
: : :
: : : {                      // 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
: : : 

: : :
: : : In C you would use malloc/free instead of new/delete.
: : :

: : :
: :
: : in addition, u shud never try to deallocate memory which hasn't allocated by ur program, or already deallocated! as in ur example,
: :
: : int *myIntPointer;[/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,
: :
: : delete myIntPointer;[/b]
: :
: : will try to deallocate the contents of memory of that garbage location. this may crash the program.
: :

: :
~Donotalo()
: :
: :
:
: 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:

#define NULL ((void*)0)

or it may be something else. Yep, really fuzzy.
In C++, NULL is always 0.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.