Я получаю ошибку Mallo c при попытке динамически распределить память. Я разместил свою полную программу - PullRequest
0 голосов
/ 01 марта 2020

1.Почему произошла следующая ошибка

"mallo c. c: 2374: sysmallo c: утверждение` (old_top == (((mbinptr) ((( char *) & ((av) -> bins [((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) )

= (без знака long) ((((__buildin_offsetof (struct malloc_chunk, fd_nextsize)) + ((2 * (sizeof (size_t))) - 1)) & ~ ((2 * ( sizeof (size_t))) - 1))) && ((old_top) -> size & 0x1) && ((unsigned long) old_end & pagemask) == 0) 'ошибка. "
Прервано (ядро выгружено)

2.i попытался создать массив Dynami c с некоторыми функциями. Когда предыдущий размер массива stati c заполнен, я попытался динамически создать новый массив и назначить эту память указателю который указывает на старый массив. Но я получаю вышеупомянутую «ошибку» при этом. Помогите мне выяснить, где я go ошибся.

#include<iostream>

class arrays
{
private:
   int* array_1;
   int sizeofarray;
   int currentsize;
public:

arrays()
{
   currentsize=0;
   sizeofarray=10;
 array_1=new int[sizeofarray];

}
void inserter(int value)
{
    if(currentsize<=sizeofarray)
    {
    array_1[currentsize]=value;
    currentsize=++currentsize;
    }
    else
    {
       int* temp=array_1;
       int* array_1=new int[20];//this is were error occurs.kindly help me understand why?
       int i=0;//also suggest me where i should change the code in-order to work correctly
       for(;i<=sizeofarray;i++)
       {
       array_1[i]=temp[i];
       }
       sizeofarray=sizeofarray*2;
       array_1[i]=value;
       currentsize++;
    }
}
void printall()
{
   for(int i=0;i<=currentsize;i++)
    {
      std::cout<<array_1[i]<<std::endl;
    }
}

void modify(int position,int value)
{
   array_1[position]=value;
}
int getsize()
{
   return currentsize;
}
int getvalue(int position)
{
  std::cout<<array_1[position];
}
};



int main()
{
   arrays n1;
   for(int i=0;i<20;i++)
   n1.inserter(i);
   std::cout<<n1.getsize()<<std::endl;
   n1.printall();
   for(int position=0;(position%2==0 && position<=n1.getsize());position++)
   n1.modify(position,++position);
   n1.getvalue(10);
   n1.printall();
}
...