10

Is it safe and/or good practice to do something as the following?

//NewList is a member function of a class
void NewList(int size){

delete[] list; //list is a member variable; an already initialized dynamic array.

list=new ListObject[size];

}

I'm basically discarding the previous array because I will use different data to store in the class, and hence require a new list to store other information on the new data. If this is not good practice, what's the alternative?

8
  • Did you read the Rule of Three? It's legal to assign a pointer to new memory, whether or not it's safe depends on the rest of the code. Commented Feb 7, 2015 at 6:52
  • "Is it safe and/or good practice to do something as the following?" Yes, it is safe. Whether it is good is subject to opinions. Commented Feb 7, 2015 at 6:52
  • 1
    What you are doing is perfectly acceptable if you need to use an array (unlikely). Otherwise use a std::vector Commented Feb 7, 2015 at 7:00
  • Okay, I just wasn't sure if delete was going to invalidate the pointer, so when I want to reuse it, I thought maybe I should keep a list of allocated memory addresses and only use delete on the whole list before the end of the program. I'll read the link "Rule of Three" in a few moments. Commented Feb 7, 2015 at 7:02
  • 5
    I hate stupid answers like "use a vector". That wasn't your question. Yes, it's perfectly safe and perfectly appropriate to assign a new object to a variable you've already "deleted". Commented Feb 7, 2015 at 7:10

3 Answers 3

4

It depends. Every time you create an object with new, you must delete it after use. In the given function you are deleting to create a new one, but are you also deleting once you are done with the object? It's safer to create your object and have the system delete it when the object is out of scope.

I would avoid if possible as you can create a memory leak if not deleted appropriately.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, it's totally fine to reuse a pointer to store a new memory address after deleting the previous memory it pointed to.

Just be careful not to dereference the old memory address which is still stored in the pointer. In your code snippet that's not a problem though.

As a side note, most of the time, you should use std::vector if you want a dynamically-allocated array, as mentioned in the comments.

Comments

1

There is nothing inherently wrong with what you're doing. However, if it is a member function of a class, and list is a member variable, be aware that the code you have is not exception safe.

In other words, if the call to new[] fails for some reason, your list array has been destroyed, and you can't recover the data.

Better to do this:

void NewList(int size)
{
    ListObject* temp = new ListObject[size]; 
    delete[] list; 
    list = temp;
}

If the call to new[] throws an exception, you haven't destroyed your original data.

However, all of this is taken care of if you used std::vector, as others have suggested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.