gpt4 book ai didi

c++ - 段错误错误c++动态数组

转载 作者:行者123 更新时间:2023-11-28 00:09:57 26 4
gpt4 key购买 nike

所以我正在编写一个 ArrayList 类,它在内部使用数组和一些函数来维持容量(即收缩和增长函数)。我收到一个段错误,我知道这意味着我正在尝试访问我无权访问的内存,这可能发生在我重载的 [] 运算符中,但是我似乎无法弄清楚是什么导致了这个问题。任何帮助将不胜感激!

数组列表.cpp

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

using namespace std;

int& ArrayList::operator[](unsigned int i){
try{
cout << "[] called: i = " << i << ".........return val = " << foo[i] << endl ;
if(i >= numElements)
throw 1;
return foo[i];
}
catch(int e){
cout << "An error has occured. Error code " << e;
switch(e) {
case 1:{
cout << ". You may be trying to access an index that doesn't currently exist in the arraylist!" << endl;
break; //1 = IndexOutofBoundException
}
}
}
}
int ArrayList::size(){
return numElements;
}
void ArrayList::shrink(){
int* bar = new int[capacity/2]; //temp array to hold values while I decrease the size
for(int i = 0; i < capacity/2; i++){
bar[i] = foo[i];
}
delete foo;
foo = bar;
capacity /=2;
}

void ArrayList::grow(){
int* bar = new int[capacity*2]; //temp array to hold values while I increase the size
for(int i = 0; i < capacity; i++){
bar[i] = foo[i];
}
for(int i = capacity; i < capacity*2;i++){
bar[i] = 0;
}
delete foo;
foo = bar;
capacity *=2;
}

void ArrayList::push_back(int m){
if(numElements == capacity) //full, double foo
grow();
foo[numElements] = m;
numElements++;
}

void ArrayList::erase(int m){
bool notFound = true;
int i = 0;
while(notFound){
if(foo[i] == m){
notFound = false; //Ha! Found you!
for(int j = i; j < capacity; j++){
foo[j] = foo[j+1]; //moves everything to right of m one spot left
numElements--;
}
}
else
i++; //keep looking
}
if(numElements*2<capacity)
shrink();
}

string ArrayList::toString(){
stringstream sobj;
string x;
sobj << "[";
for(int i = 0; i < numElements; i++){
if(i == numElements-1) //last iteration, avoids output displaying [1,2,3,4,]
sobj << foo[i];
else
sobj << foo[i] << ",";
}
sobj << "]";
sobj >> x;
return x;
}

ArrayList::ArrayList(){
capacity = 1;
numElements = 0;
foo = new int[1];
foo[0] = 0;
}

ArrayList::~ArrayList(){
delete foo;
cout << "Destructor called" << endl;
}

数组列表.h

#ifndef _ARRAYLIST_H_
#define _ARRAYLIST_H_
#include <string>
class ArrayList
{
public:
ArrayList();
~ArrayList();

int& operator[](unsigned int i);

void push_back(int m);
void erase(int m);
std::string toString();
int size();

private:
void shrink();
void grow();

private:
int capacity, numElements;
int* foo;
};

#endif

测试.cpp

#include "ArrayList.h"
#include <iostream>
using namespace std;

int main(int argc,char *argv[])
{
ArrayList arr;

for (int i=1;i<=50;i++)
{
arr.push_back(i);
}

cout << "Should contain numbers 1..50, is ";

cout << arr.toString() << endl;

for (int i=arr.size()-1;i>=1;i--)
{
arr.erase(arr[i]);
}

cout << "Should contain only 1, is ";
cout << arr.toString() << endl;

arr.erase(arr[0]);

for (int i=2;i<=50;i++)
{
if (i<=2)
arr.push_back(i);
else
{
int j=0;
while ((j<arr.size()) && (i%arr[j]!=0))
j++;

if (j==arr.size())
{
arr.push_back(i);
}
}
}

cout << "Prime numbers between 1 and 50 are: " << arr.toString() << endl;

}

最佳答案

函数 ArrayList::erase() 有错误。

void ArrayList::erase(int m){
bool notFound = true;
int i = 0;
while(notFound){
if(foo[i] == m){
notFound = false; //Ha! Found you!
for(int j = i; j < capacity; j++){
foo[j] = foo[j+1]; //moves everything to right of m one spot left

//========================================
// Problem.
// This is in the wrong place.
// It needs to be outside the for loop.
//========================================
numElements--;
}
}
else
i++; //keep looking
}
if(numElements*2<capacity)
shrink();
}

固定函数:

void ArrayList::erase(int m){
bool notFound = true;
int i = 0;
while(notFound){
if(foo[i] == m){
notFound = false; //Ha! Found you!
for(int j = i; j < capacity; j++){
foo[j] = foo[j+1]; //moves everything to right of m one spot left
}

//========================================
// It needs to be outside the for loop.
//========================================
numElements--;
}
else
i++; //keep looking
}
if(numElements*2<capacity)
shrink();
}

顺便说一句,我能够通过添加一行来在删除项目后立即打印数组的内容来快速识别问题。

for (int i=arr.size()-1;i>=1;i--)
{
arr.erase(arr[i]);
cout << arr.toString() << endl;
}

该函数的一个更健壮的版本是:

void ArrayList::erase(int m){    
for ( int i = 0; i < numElements; ++i )
{
if(foo[i] == m){

// You need to move only the number of
// items of the array that have user set
// numbers.
for(int j = i; j < numElements-1; j++){
foo[j] = foo[j+1];
}

// This is strictly not necessary but is
// in the spirit of rest of your code where
// you initialize to zero all members that have
// not been explicitly set by the user.
foo[numElements-1] = 0;

numElements--;
break;
}
}

if(numElements*2 < capacity)
shrink();
}

关于c++ - 段错误错误c++动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com