Thread: How to use new and delete?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    How to use new and delete?

    I am beginner with C++ and I want to know. When do I use new and delete keywords? And what can I use them to do? If anyone has any website that has a challenge to do with new and delete please post the link! . The only thing I know about the new keyword and delete is that you can do this:

    Code:
    int Size = 5;
    int *ptr = new int(Size);
    cout << *ptr << endl;
    delete ptr;
    And more . . .

    But when do you use this keywords? I am just curios about them and want to now if them can be really effective to use. Thanks for replies!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DecoratorFawn82
    When do I use new and delete keywords? And what can I use them to do?
    Generally, you would not use them directly, or if you do use new directly, you would hand the result to a smart pointer shortly afterwards. If you do use them directly, it would likely be because you are implementing a container or smart pointer, or have some other special requirements.

    I suggest that you work through an introductory book like Accelerated C++ which will first show you how to use things like standard containers and then how to work with pointers along with new and delete.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by DecoratorFawn82 View Post
    Code:
    int Size = 5;
    int *ptr = new int(Size);
    cout << *ptr << endl;
    delete ptr;
    this is a perfectly valid use of these keywords. if you allocate an array, you need to remember to delete the whole array with delete[].

    Code:
    int Size = 5;
    int *ptr = new int[Size];
    for (int i = 0; i < Size; ++i)
    {
      cout << *ptr << endl;
    }
    delete[] ptr;
    like laserlight says though, use a smart pointer or the appropriate container, instead of raw pointers and dynamic arrays. you'll save yourself a lot of headaches and debugging.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int Size = 5;
    int *ptr = new int[Size];
    for (int i = 0; i < Size; ++i)
    {
      cout << *ptr << endl;
    }
    delete[] ptr;
    With these modifications you just made the code meaningless...

    Instead of unlike the 1st one - printing the value of initialized dynamically allocated variable, you now

    printing 5 times the 1st member of uninitialized dynamic array.

    You would better of with putting ... inside the loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    okay so I missed a change to one line. the point was to demonstrate creating and deleting dynamic arrays, which I did.

    don't be so pedantic.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elkvis View Post
    okay so I missed a change to one line. the point was to demonstrate creating and deleting dynamic arrays, which I did.
    Your code demonstrated little of use relevant to using said dynamically allocated arrays. Since the array contents are uninitialised, your loop exhibits undefined behaviour, which is hardly a good thing to demonstrate without explanation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Thanks for replies everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concerning delete/delete[] at program exit
    By laserlight in forum C++ Programming
    Replies: 58
    Last Post: 01-09-2008, 01:40 PM
  2. Replies: 17
    Last Post: 11-16-2006, 09:06 PM
  3. delete []
    By Zahl in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2002, 08:11 AM
  4. how to delete ?
    By abhiboy007 in forum C++ Programming
    Replies: 1
    Last Post: 05-22-2002, 09:03 AM
  5. using delete to delete an array
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2002, 03:53 PM