在CodeBlocks中對此進行編碼,該程序不斷收到seg故障。誰能幫忙?程序不斷收到分段錯誤
#include <string>
#include <iostream>
#include <sstream>
#include "include/hugeint.h"
using namespace std;
int main(int argc, char *argv[])
{
HugeInt h("123");
return 0;
}
我HugeInt類(什麼相關),存儲非常大的整數作爲整數
class HugeInt
{
public:
HugeInt(string);
private:
DynArray dyn;
//HugeInt& reverse();
int size;
};
HugeInt::HugeInt(string input)
{
string digits = input;
for(unsigned int i = 0; i < digits.length(); i++){
dyn.add(digits.at(i) - 48);
}
size = dyn.size();
}
我的動態數組類整數
class DynArray
{
public:
DynArray();
~DynArray();
private:
int length;
int *arr; //points to this array
int nextIndex;
};
DynArray::DynArray() {
arr = new int[10];
for (int i = 0; i < 10; i++)
arr[i] = 0;
length = 10;
nextIndex = 0; }
DynArray::~DynArray()
{
delete [] arr;
}
int DynArray::size(){
return nextIndex;
}
void DynArray::add(int val) {
int *newArr;
if (nextIndex == length) {
length = length + 10;
newArr = new int[length];
for (int i = 0; i < nextIndex; i++)
newArr[i] = arr[i];
for (int j = nextIndex; j < length; j++)
newArr[j] = 0;
delete [] arr;
arr = newArr;
}
arr[nextIndex++] = val;
}
編輯的數組:我評論刪除[ ] arr;它仍然seg故障:/
編輯2:好,所以如果main如下代碼工作。任何人都可以解釋爲什麼請問
#include <string>
#include <iostream>
#include <sstream>
#include "include/hugeint.h"
using namespace std;
int main(int argc, char *argv[])
{
string in = "1234";
HugeInt h(in);
return 0;
}
'DynArray :: add'如何實現? – Naveen
我的猜測是一個滑出界外的數組 –
它在哪裏得到這個seg錯誤?另外,如果你不是,你需要遵循三/五的規則。 – chris