c++的问题。。。困扰很久了,一直不知道哪儿出错了。。。麻烦大家了
一直报错说是,BasicDynamicArray.exe 中的 0x75be9673 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x0017f9cc 处的 std::bad_alloc。还有什么invalid allocation size。。。晕死了,求大家看看下面的代码啊。。。。#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "efficientArray.h"
using namespace jj;
EfficientArray::EfficientArray(int size, double value)
{
m_size=size;
if(m_size>m_capacity){
reserve(m_size,value);
}
}
EfficientArray::EfficientArray(const EfficientArray& ba)
{
m_size=ba.m_size;
if(m_size>m_capacity){
reserve(m_size,0);
}
for(int i=0;i<m_size;++i){
m_data[i]=ba.m_data[i];
}
}
EfficientArray& EfficientArray::operator = (const EfficientArray& array)
{
if(this!=&array){
m_size=array.m_size;
if(m_size>m_capacity){
reserve(m_size,0);
}
for(int i=0;i<m_size;++i){
m_data[i]=array.m_data[i];
}
}
return *this;
}
double EfficientArray::at(int ind)
{
if(ind<0||ind>m_size-1){
printf("Error");
return 0;
}
else{
double result=m_data[ind];
return result;
}
}
double EfficientArray::operator[] (int ind) const
{
if(ind<0||ind>m_size-1){
printf("Error");
return 0;
}
else{
double result=m_data[ind];
return result;
}
}
double& EfficientArray::operator[] (int ind)
{
return m_data[ind];
}
int EfficientArray::push_back(double elem)
{
++m_size;
if(m_size>m_capacity){
reserve(m_size,0);
m_data[m_size-1]=elem;
}
else{
m_data[m_size-1]=elem;
}
return m_size;
}
int EfficientArray::insert(int ind, double value)
{
if(ind<0||ind>m_size-1){
printf("Error!");
return 0;
}
else{
++m_size;
if(m_size>m_capacity){
reserve(m_size,0);
for(int i=m_size-1;i>ind;--i){
m_data[i]=m_data[i-1];
}
m_data[ind]=value;
}
else{
for(int i=m_size-1;i>ind;--i){
m_data[i]=m_data[i-1];
}
m_data[ind]=value;
}
return m_size;
}
}
int EfficientArray::reserve(int num, double value)
{
if( num<m_capacity )
{
printf("too small!");
exit(1);
}
int capacity = m_capacity+num;
int size = m_size;
double* tmp;
tmp = new double[capacity];
memcpy(tmp, m_data, size*sizeof(double));
for(int i=size; i<capacity; ++i)
{
tmp[i] = value;
}
if(m_data!=NULL)
delete[] m_data;
else exit(1);
m_size = size;
m_capacity = capacity;
m_data = tmp;
return m_capacity;
}
bool EfficientArray::isValidateIndex(int ind)
{
return false;
}
void EfficientArray::print()
{
for(int i=0;i<m_size;++i){
printf("%f ",m_data[i]);
}
printf("\n");
}
void jj::insertionSort(EfficientArray& a)
{
int i,j;
for(i=1;i<a.size()-1;++i){
double tmp=a[i];
j=i-1;
while(j>=0&&tmp<a[j]){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=tmp;
}
}