0

So I'm trying to create a function that "resizes" a member array to a new size passed as an argument. By "resize", I mean that it should set the member array to a new array with the new size, copy over the elements from the old array, and then deallocate the memory associated with the old array. Here's what I have so far:

void MemoryTest::resize(unsigned int new_size) {
    if (size == new_size)
        return;

    int* oldPtr = elements;

    elements = new int[new_size + 1];
    for (int i = 0; i < (new_size < size) ? new_size : size; i++)
        elements[i] = oldPtr[i];

    elements[new_size] = '\0';

    if (size > new_size)
        size = new_size;

    delete[] oldPtr; // Deallocate old elements array
}

elements is a private member int* initialized to NULL.

However, when it begins the for loop, the program hangs for awhile before giving an Access Violation for the elements[i] = oldPtr[i] line. Someone please correct me if I'm wrong (which I probably am), but my understanding is that oldPtr should be a pointer pointing to the same initial point as elements. Then, I set elements equal to a new array, so the two are now pointing to two different things. Then I iterate through elements, setting each item equal to its counterpart in the old array.

Also, while I would normally use a vector to avoid situations like this, I'm trying to become more familiar with pointers and memory allocation in C++.

Thanks in advance!

5
  • 1
    Your member variable size is not accurate after the first call to this function. I can't determine if it's accurate before. Commented Jan 26, 2015 at 5:27
  • And now that you've changed the code, your member variable size will be wrong when the array is made larger. Commented Jan 26, 2015 at 5:32
  • I'm trying to make size reflect the number of elements, not the number of memory spots in the array, so it should only increase when new elements are added. That being said, the problem did lie in size, which was causing a problem with iterating through the for loop. Commented Jan 26, 2015 at 5:35
  • 1
    @user2884505 - I'm trying to make size reflect the number of elements, not the number of memory spots in the array, That's why vector has both size and capacity members. Commented Jan 26, 2015 at 5:38
  • I guess another reason to just use vectors haha. Thanks for the clarification! Commented Jan 26, 2015 at 5:40

1 Answer 1

3

for (int i = 0; i < (new_size < size) ? new_size : size; i++)

should read

for (int i = 0; (i < (new_size < size)) ? new_size : size; i++)

So, for (int i = 0; i < ((new_size < size) ? new_size : size); i++) should correct your code.

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

4 Comments

Why not just a simple for (int i = 0; i < size; ++i)?
@PaulMcKenzie that may be too many iterations for the new array.
@DrewDormann - Of course, I'm assuming that size remains the same until after all of the dynamic memory shenanigans is completed, and at the end of the function, size is set correctly to the new size.
Thank you, this did it! I have to wait two minutes to accept this answer, but this was one of the smallest wait times I've had lol. I really appreciate it everyone!

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.