[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Where is the error?

Here is a header file for a simple ADT list class that I am trying to implement (you will need to log in to download it and view it).

https://filedb.experts-exchange.com/incoming/ee-stuff/67-ExList.txt

The function I am trying to implement is the append function. This function will add an item to the list - but if the list is full, it will create a new list equal to the old list. The size of the new list is the original size + growAmount.

void ExList::append(ItemType newItem)
{
      if (length < capacity){
            //the list is not full, so we just add the item to the next position
            *(items+length) = newItem;
            length++;
      }
      else{
            //the list is full, so we need to create a new list that has size
            //equal to capacity + growAmount

            //copy the original list
            ExList New (*this);
            delete[] items;

                              //--- stops working here .. the if statement will not evaluate to true

            //create memory to store new list with size = capacity + growAmount
            if (items = new ItemType [capacity + growAmount]){
                  //copy the old data to the new list
                  for(int i=0;i<New.length;i++)
                        *(items+i)=New.get(i);
                  //add the new item. this is at position
                  items[New.capacity+1] = newItem;
            }
            else{
                  cout<<"ExtList: unable to allocate memory-append\n";
            }
      }

}


Any idea what is wrong here? The program outputs "ExtList: unable to allocate memory-append\n"
0
bingie
Asked:
bingie
  • 4
  • 3
1 Solution
 
PaulCaswellCommented:
Hi bingie,

1. You have set growAmount to a reasonable value havent you?

2. I notice you are not adding growAmount to capacity when you grow the array. That cant be right.

3. You also dont seem to be incrementing length when you grow, even though you add an item in this case. The better technique in this case is to code it like:

if ( <need to grow> )
{
  <grow>
}
<put new item on end>

Paul
0
 
bingieAuthor Commented:
growAmount is the amount the list needs to grow when the length = capacity. I do update the length variable when I append, and then check to see if it is at max before appending again.



void ExList::append(ItemType newItem)
{
      if (length < capacity){
            //the list is not full, so we just add the item to the next position
            cout<<"(append) - Shrink now is"<<shrinkAmount<<endl;
            *(items+length) = newItem;
            length++;
      }
      else{

            //the list is full, so we need to create a new list that has size
            //equal to capacity + growAmount

            //copy the original list
            ExList New (*this);
            delete[] items;

            //create memory to store new list with size = capacity + growAmount
            if (items = new ItemType [capacity + growAmount]){
                  //copy the old data to the new list
                  for(int i=0;i<New.length;i++)
                        *(items+i)=New.get(i);
                  //add the new item. this is at position
                  items[New.capacity+1] = newItem;
                  capacity +=growAmount;
                  length++;

            }
            else{
                  cout<<"ExtList: unable to allocate memory-append\n";
            }
      }

}
0
 
bingieAuthor Commented:
This part will not evaluate to true - ever it seems

 if (items = new ItemType [capacity + growAmount]){
               //copy the old data to the new list
               for(int i=0;i<New.length;i++)
                    *(items+i)=New.get(i);
               //add the new item. this is at position
               items[New.capacity+1] = newItem;
               capacity +=growAmount;
               length++;

          }
          else{
               cout<<"ExtList: unable to allocate memory-append\n";
          }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PaulCaswellCommented:
>>if (items = new ItemType [capacity + growAmount]){
Yes indeed! Thus my first point. The only way for 'new' to fail and return null is if memory is either unavailable or corrupt or you are asking for a stupid amount.

While you're there, check what 'capacity' is initialised to.

Paul
0
 
bingieAuthor Commented:
Well, it turns out that my instructor gave the vaiable names in the constructo the same name as the members of the class and i neglected to use the "this->" operator.

So, the values were never initialzed properly.
0
 
PaulCaswellCommented:
That one is so common there should be a compiler warning for it. Not easy to find either sometimes. Glad you've cracked it! :-)

Paul
0
 
GranModCommented:
Thanks Paul.

Keep up the good work !

GranMod
The Experts Exchange
Community Support Moderator of all Ages
0
 
PaulCaswellCommented:
Sorry, I was going to paq-refund it too for you but we had a network brownout due to a thunderstorm.

Keep up the good work! :-)

Paul
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now