1

I have to write a program, where i use dynamic arrays:

int size=1;
double* dyn_arr = new double[size];

int n=0; 
double sum=0.0;
while(std::cin >> dyn_arr[n]){
    sum = sum + dyn_arr[n++];
    if(n==size){
        size*=2;
        double* new_array = new double[size];
        std::copy(dyn_arr, dyn_arr + n, new_array);
        delete[] dyn_arr;
        dyn_arr = new_array;
    }
}

I can't understand the part dyn_arr = new_array after delete[] dyn_arr. dyn_arr is a pointer on the first element of the array dyn_arr, isn't it? How can i delete the pointer/array and write again dyn_arr = new_array ?

7
  • I'm not sure I understand what you're asking. It seems like you showed how to do something in code and then asked how to do it. Umm .. with the code you showed. Is there some line of code whose function you don't understand? Commented May 12, 2016 at 9:02
  • after delete[] dyn_arr the array where dyn_arr pointed to is freed and dyn_arr is a dangling pointer. you can reassign to this pointer, e.g. nullptr or a pointer to another valid address like new_array Commented May 12, 2016 at 9:03
  • thank you, that was the question. I saw it in the theory part but i wanted to ask again for confirmation. Commented May 12, 2016 at 9:03
  • @Susliks, double* dyn_arr is a pointer. A pointer is a variable which holds a memory address. what dyn_arr=new_array does is set the memory address required to be a different one (which holds a bigger buffer). Commented May 12, 2016 at 9:05
  • 1
    C++ education advice: Don't expect too much from the course, if this is how C++ is taught. delete[ ] should be in a destructor, not in the same function as new[ ]. But there's already std::vector. so you shouldn't be reinventing dynamic arrays. new[] is a rare, primitive building block. Commented May 12, 2016 at 9:29

3 Answers 3

5

The dyn_arr variable is a variable of type "pointer to double". That means it holds the address of a double and you can change which double it points to whenever you want.

double* dyn_arr = new double[size];

Now, dyn_arr points to the first of size doubles.

double* new_array = new double[size];

This creates a new variable of type "pointer to double" that points to the first of a new array of doubles.

delete[] dyn_arr;

This deletes the array of doubles dyn_arr points to. Since we got dyn_arr's value from new[], we can pass it to delete[] when we're done with the array. So now dyn_arr points to garbage and we mustn't use its value.

dyn_arr = new_array;

This changes the value of dyn_arr so it points to the new set of doubles we allocated. Since dyn_arr is a variable of pointer type, we can change it to point to different things whenever we want.

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

Comments

3
delete[] dyn_arr;

You're not "deleting the pointer". You're "deleting" the memory that it used to point to. Now it points to nothing. It's dangling.

dyn_arr = new_array;

Now you're making the pointer point to the new block of memory, instead.

Comments

1

new_array is a new dynamic array that was created inside your if statement. The lines in question are deleting the original array, and then assigning dyn_array to point to the new array.

delete[] dyn_arr;     // <--- Delete the old array.
dyn_arr = new_array;  // <--- Point dyn_arr at the newly allocated array.

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.