0

I am fairly new to c++, as this is the first class I am taking of it. I am creating a class that will dynamically create/delete an array based on what operations are performed. I am getting this exception when I try to add something to the array I believe. Any help is much appreciated.

Code:

#include "ArrayList.h"
#include < string>

using namespace std;

int count = 0;

 ArrayList::ArrayList(){
Object *items = new Object[count];
items[0] = "NULL";
}    

 ArrayList::~ArrayList(){
 releaseList();
 }

void ArrayList::releaseList(){
delete[] items;
count = 0;
ArrayList();
}

void ArrayList::add(Object o){
Object *temp = new Object[count + 1];
for(int i = 0; i < count; i ++){
    temp[i] = items[i];
}
temp[count + 1] = o;
delete[] items;
items = temp;
delete[] temp;
count = count + 1;
}

void ArrayList::add(Object o, int index){
Object *temp = new Object[count + 1];
for(int i = 0; i < index; i++){
    temp[i] = items[i];
}
temp[index] = o;
for(int i = index + 1; i < count -1; i ++){
    temp[i] = items[i];
}
delete[] items;
items = temp;
delete[] temp;
count = count + 1;
}

The .h file for ArrayList

#ifndef ARRAY_LIST_H
#define ARRAY_LIST_H

#include <string>
using namespace std;

typedef string Object;


class ArrayList {
private:
Object *items;      // a dynamic array of pointers to Objects
int numberOfItems;

// Releases all the memory allocated to the list; resets the object to its
// default values.
void releaseList();

public:
// Initializes object to default values (NULL, 0).
ArrayList();

// Destroys the object. Calls releaseList() to do the actual work.
~ArrayList();

// Appends the object o to the end of the list. This resizes the array by 1.
void add(Object o);

// Adds the object o and the specified index. This resizes the array by 1.
void add(Object o, int index);

// Removes all the items from the list. Resets object to its default state.
void clear();

// Returns true if the list contains the object o.
bool contains(Object o) const;

// Returns the object at the specified index. Assumes index is in range.
Object objectAt(int index) const;

// Returns the index of the first matching object o. Assumes the object is in the list.
int indexOf(Object o) const;

// Returns the index of the last matching object. Assumes the object is in the         list.
int lastIndexOf(Object o) const;

// Returns true if list is empty.
bool isEmpty() const;

// Removes the object o from the list. List is resized. Assumes object is present.
void remove(Object o);

// Removes the object o at the specified index. List is resized. Assumes index is in range.
void remove(int index);

// Returns the number of elements in the list.
int size() const;

// Sets the element at the specified index.
void set(Object o, int index);

// Returns a string containing all the items comma separated and surrounded
// by curly ({) braces, i.e. {item1, item2, ...}
string toString();
};

#endif
8
  • First-chance exception at 0x0fb1ca58 (msvcr100d.dll) in hw01.exe: 0xC0000005: Access violation writing location 0xabababab. Unhandled exception at 0x775c15de in hw01.exe: 0xC0000005: Access violation writing location 0xabababab. Commented Jun 2, 2013 at 23:34
  • items[0] = "NULL";? You meant items[0] = NULL right? Commented Jun 2, 2013 at 23:35
  • Yes, that was giving me an exception earlier, I just had not changed it. Commented Jun 2, 2013 at 23:36
  • 1
    Can you add the declaration of the class also? That said, I think your add function does delete the freshly created array when it does the delete[] temp; so from that point on you're dealing with undefined behaviour. Commented Jun 2, 2013 at 23:38
  • the .h file, or the driver file? Commented Jun 2, 2013 at 23:39

1 Answer 1

3

This is a problem, in ArrayList::add, that will throw an access violation exception:

Object *temp = new Object[count + 1];
...
temp[count + 1] = o;

You're assigning an object to a memory location one past the end of the array. C++ is zero-indexed, meaning if you have

int A = new int[5];

then valid locations are A[0] ... A[4], but not A[5].

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

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.