A Few Questions Concerning New and Delete.

This is a discussion on A Few Questions Concerning New and Delete. within the C++ Programming forums, part of the General Programming Boards category; This is different from my last thread concerning new and delete. I have the proper syntax, and the program compiles ...

  1. #1
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86

    A Few Questions Concerning New and Delete.

    This is different from my last thread concerning new and delete. I have the proper syntax, and the program compiles perfectly fine, but even after I 'delete' my new intenger, it still works when I print it to the screen.

    1.) #include <iostream.h>
    2.) main()
    3.) {
    4.) int *a = new int; //Creates New Intenger 'a'
    5.) cout << a;
    6.) delete a;//Deletes Intenger 'a
    7.) cout << a;//I thought this wouldn't work, but it did.
    8.) return 0;
    9.) }

    Is the reason for this because 'delete' doesn't actually delete the new intenger, but it actually de-allocates the memory that was allocated for it?

    Another thing: Printing 'a' to the screen only displays it's memory address. How do I create a new intenger that can be printed to the screen with it's value, rather than it's address?

    Thanks for your time.
    - Dean

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: A Few Questions Concerning New and Delete.

    Originally posted by LordVirusXXP
    This is different from my last thread concerning new and delete. I have the proper syntax, and the program compiles perfectly fine, but even after I 'delete' my new intenger, it still works when I print it to the screen.

    1.) #include <iostream.h>
    2.) main()
    3.) {
    4.) int *a = new int; //Creates New Intenger 'a'
    5.) cout << a;
    6.) delete a;//Deletes Intenger 'a
    7.) cout << a;//I thought this wouldn't work, but it did.
    8.) return 0;
    9.) }

    Is the reason for this because 'delete' doesn't actually delete the new intenger, but it actually de-allocates the memory that was allocated for it?

    Another thing: Printing 'a' to the screen only displays it's memory address. How do I create a new intenger that can be printed to the screen with it's value, rather than it's address?

    Thanks for your time.
    when you delete a;, you mark the memory it had as available. it doesnt change the value of a itself though. it's your responsibility to ensure you don't use it after deletion.

    to modify or display the int that a points to instead of the address, you need to derefrence it:

    Code:
    int *a;
    a = new int;
    cout << a; // address
    *a = 55; // number
    cout << *a; // number
    delete a;
    hello, internet!

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: A Few Questions Concerning New and Delete.

    Originally posted by LordVirusXXP
    Is the reason for this because 'delete' doesn't actually delete the new intenger, but it actually de-allocates the memory that was allocated for it?

    As far as I know that's right.
    none...

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Yeah -- it'd just be a waste of time to change the memory to 0s or something else and there'd be no reason to, so it doesn't. It's the same reason that variables on the stack aren't initialized to 0.

  5. #5
    epoch
    Guest
    Just as a note, if you nullify the pointer, you will not be able to use it anymore. ie:
    a=NULL;
    cout << *a; // SIGSEGV (don't do it lol)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21