deleting pointers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

deleting pointers

digitalranadigitalrana Posts: 19Member
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?


Comments

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : 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]
  • DonotaloDonotalo Posts: 715Member
    : : 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]

  • bluj91bluj91 Posts: 133Member
    : : : 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.
  • LundinLundin Posts: 3,711Member
    : : : : 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:

    #define NULL ((void*)0)

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