0

My program works but my professor says that my code is incorrect but stated that he will get to why in the fall term... What is he talking about? perhaps something is improper? Even if you are incorrect I would appreciate picking your brain :)

void CResizableArray::SetSize( int intNewSize )
{
    int intIndex = 0;

    if( intNewSize < 0 ) intNewSize = 0;
    if( intNewSize > intMAXIMUM_ARRAY_SIZE )
    {
        intNewSize = intMAXIMUM_ARRAY_SIZE;
    }

    //////////////////////////////////////
    //     ---> HUGE BUG HERE <---      //
    //     Code works but is WRONG      //
    // WHY IS THIS HELP ME FIND THE BUG //
    //////////////////////////////////////

    m_intArraySize = intNewSize;
    m_paintValues = new int [m_intArraySize];

    // Initialize to zero
    for( intIndex = 0; intIndex < m_intArraySize; intIndex++ )
    {
        *( m_paintValues + intIndex ) = 0;
    }
}
4
  • Is there any particular reason you're using *( m_paintValues + intIndex ) instead of m_paintValues[intIndex]? Was it just to explore array-pointer equivalence? Better yet would be new int [m_intArraySize](), which would zero-initialize it for you. Commented Jun 13, 2012 at 3:42
  • Is there any reason you are introducing bugs when you could be using std::vector? Commented Jun 13, 2012 at 3:44
  • I'm not familiar with that method, could you possibly enlighten me? Commented Jun 13, 2012 at 6:52
  • please keep in mind this is just a single subroutine in my program I have a CResizableArray.cpp, CResizableArray.h and CHomework8.cpp file This particular subroutine is made ONLY to set the size of the array dynamically. It has been written this way for boundary checking. Commented Jun 13, 2012 at 7:00

2 Answers 2

8

Presumably before this line

m_paintValues = new int [m_intArraySize];

m_paintValues pointed to another array. That array has now been leaked -- you don't have a pointer to it, so it can never be freed. That memory can therefore never be reused. Write a program that does a lot of this, and it'll run out of memory before running very long.

When you're through with a block of memory, you need to free it. Here, the proper thing to do might look something like

delete[] m_paintValues;
m_paintValues = new int [m_intArraySize];

There are more issues, though. First of all, you can never use delete[] unless you know that m_paintValues definitely points to an array; you could ensure that in the constructor. More troubling is that fact that when you set a new size, any data previously in m_paintValues is discarded -- don't you want to copy the old values into the new array? Doing so would mean using a temporary variable to hold the new array when first allocated, copying the data, and then assigning the new array to the member variable.

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

2 Comments

Wow that's great also you can tell its pointing to an array because of my prefixing methods that is actually intValues as in an integer named Values with a meaning array and p meaning pointer we know it is a pointer array int Values or paintValues
@NethMunson -- I didn't mean that you needed to be able to tell what type of variable it is; I meant that you can't call delete[] unless you know that the value of the variable is actually a valid pointer to a real array at the time that you make the call. If the pointer is 0 or otherwise points to garbage or to a previously freed array, then calling delete[] will probably crash the program.
2

He may mean that since it is a resize you should keep the old contents of the array and transfer them over to the new array, in your snippet you just throw away the old content creating a new empty array.

so instead of

m_paintValues = new int [m_intArraySize];
// Initialize to zero
for( intIndex = 0; intIndex < m_intArraySize; intIndex++ )
{
    *( m_paintValues + intIndex ) = 0;
}

do

int* newBiggerArray = new int[m_intArraySize];
for (intIndex = 0; intIndex < m_intArraySize; ++intSize)
{
   if ( intIndex < oldMaxSize )
   {
     newBiggerArray[intIndex] = m_paintValues[intIndex];
   }
   else
   {
     newBiggerArray[intIndex] = 0;
   }
}
delete [] m_paintValues;
m_paintValues = newBiggerArray;

I will leave the part to handle a resize to a smaller value than previous for you to figure out.

1 Comment

unfortunately no that is not the purpose of this program. My program does exactly what it was built to do, we are just now learning about pointers so he kept it simple.

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.