# resizing dynamic arrays

## Recommended Posts

I am attempting to make a class that does the exact same thing as a standard vector in C++. I can get everything to work with the execption of resize and reserve functions. This is an exercise in learning how to allocate memory correctly using dynamic arrays. There are 3 private variables that are max_count for capacity and count for size of the current dynamic array. Here is my reserve function
void VectorDouble::reserve(int capacity)
{
if(capacity < max_count)
return;
else
{
max_count = capacity;
double *temp = new double[max_count];
temp = elements;
elements = temp;
delete temp;
}
}

I am not sure exactly what I may or may not be doing wrong here so any advice would be great because resize function is almost exactly the same as this function. elements is a private variable that is double* elements.

##### Share on other sites
you are assigning a new array to temp and then you are changing temp to point at elements, so you just lost your new array and have no way to get it back.

what you need to do is this:

1: create the new temp array (you did this correctly already)
2: copy the values 1 by 1 from the elements array to the temp array
3: delete elements to clear up memory properly (important so you don't have useless memory bloating your application that you can't access)
4: assign the temp array to your elements array

the only tricky part is number 2 but it's fairly simple with a for loop

you will also need to do this with your resize function

hope this helps.

##### Share on other sites

double *temp = new double[max_count];
temp = elements;
elements = temp;
delete temp;

First of all, the third line is superflous: elements already contains the value of temp, since you just put the value of elements into temp in the previous line.

Second, the second line makes no sense in the first place... you JUST populated the value of temp with a double* in the previous line. Copying elements into it just leaves that double[] hanging in memory, lost.

I suspect what you are wanting to do is this:
1) Create a new array large enough to hold max_count (which line 1 does).
2) Copy the CURRENT elements into that buffer; line 2 DOES NOT DO THIS. Line 2 just points temp at the old elements, leaving the new array lost as a memory leak. You will want to do a memcopy or iterate through your elements and copy them or something else.
3) delete the original array leaving only the new larger one. Don't forget to use the correct deletion operator there, "delete [] foo"

I suggest starting with something like...

double *oldArray = elements;
elements = new double[max_count];

Then proceed from there...

##### Share on other sites
If you step through the code in your reserve() function, I think you'll see that it doesn't do what you expect (or want). Specifically, the newly allocated memory is leaked, and the memory pointed to by elements is deleted.

Also, your treatment of the vector's capacity isn't correct (at least not with respect to the C++ standard). It's a little late for a lengthy explanation, but the general idea is that std::vector makes a distinction between allocating 'raw' memory where an object of the specified type might be stored, and actually 'creating' a new object of the specified type in that location.

Doing this properly requires a fairly sophisticated understanding of how C++ manages memory and object lifetime; in particular, it (typically) involves the use of the new and delete operators, placement new, and explicit invocation of object destructors.

If I were you, I'd hold off on the reserve/capacity aspect of the implementation for now, and just focus on properly constructing, resizing, and clearing the vector, and adding and removing elements. Getting all that right will be plenty challenging without throwing low-level (well, lower-level) memory management into the mix.

##### Share on other sites
It should also be:
delete[] temp;

##### Share on other sites
Quote:
 Original post by jykIf you step through the code in your reserve() function, I think you'll see that it doesn't do what you expect (or want). Specifically, the newly allocated memory is leaked, and the memory pointed to by elements is deleted.Also, your treatment of the vector's capacity isn't correct (at least not with respect to the C++ standard). It's a little late for a lengthy explanation, but the general idea is that std::vector makes a distinction between allocating 'raw' memory where an object of the specified type might be stored, and actually 'creating' a new object of the specified type in that location.Doing this properly requires a fairly sophisticated understanding of how C++ manages memory and object lifetime; in particular, it (typically) involves the use of the new and delete operators, placement new, and explicit invocation of object destructors.If I were you, I'd hold off on the reserve/capacity aspect of the implementation for now, and just focus on properly constructing, resizing, and clearing the vector, and adding and removing elements. Getting all that right will be plenty challenging without throwing low-level (well, lower-level) memory management into the mix.

Agreed. And when you *are* ready to investigate the details, you'll honestly be better served by just looking at how std::vector actually does it.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627745
• Total Posts
2978904

• 10
• 10
• 21
• 14
• 14