【问题标题】:Memory exception when coding dynamic array C++编码动态数组 C++ 时出现内存异常
【发布时间】:2013-05-29 02:30:15
【问题描述】:

我对 c++ 还很陌生,因为这是我上的第一堂课。我正在创建一个类,它将根据执行的操作动态创建/删除一个数组。当我尝试向我相信的数组中添加一些东西时,我遇到了这个异常。任何帮助深表感谢。

代码:

#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;
}

ArrayList 的 .h 文件

#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

【问题讨论】:

  • hw01.exe 中 0x0fb1ca58 (msvcr100d.dll) 的第一次机会异常:0xC0000005:访问冲突写入位置 0xabababab。 hw01.exe 中 0x775c15de 处未处理的异常:0xC0000005:访问冲突写入位置 0xabababab。
  • items[0] = "NULL";?你的意思是items[0] = NULL 对吧?
  • 是的,之前给了我一个例外,我只是没有改变它。
  • 你也可以添加类的声明吗?也就是说,我认为您的 add 函数在执行 delete[] temp; 时确实会删除新创建的数组;因此,从那时起,您将处理未定义的行为。
  • .h 文件,还是驱动文件?

标签: c++ arrays exception


【解决方案1】:

这是一个问题,在ArrayList::add,会抛出访问冲突异常:

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

您正在将一个对象分配到数组末尾之后的一个内存位置。 C++ 是零索引的,这意味着如果你有

int A = new int[5];

那么有效位置是A[0] ... A[4],但不是A[5]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多