Problems with copying an array into another dynamic array

1

I have a problem with the execution of this program. I do not know why it ends up running so abruptly ("The program stopped working"). The results of it are what I hope, however that happens. And I tried many things to avoid it and found that what causes the error is the function void IntArr::addElement(int qtty,int *vec); because if I remove it, the program ends perfectly and without any error. Analyze the function and for me it's fine, I do not know what is slipping from my hands. The function that what must do is to pass an array with the amount of elements that I want from it and add them to another array.

PS: the original program was well divided by files (class.h, class.cpp, main.cpp), nothing more than to copy them here I had to paste everything together.

I would really appreciate your help. Greetings!

#include <iostream>
using std::cout;
using std::endl;
#define PRESS_KEY std::cout<<"\nPresione Enter para continuar . . .\n";std::cin.get();

class IntArr{
  private:
    int * p;
    int size;
    int used;
    //Verificador
    void redimensionador(int cant);

  public:
    //Constructores
    IntArr (int sz);
    IntArr (int sz,int qtty,int *vec);
    //Destructor
    ~IntArr();
    //Visualizadores
    void prtArr (void) const;
    void prtArr (int cant);
    //Accesores
    int getSize(){return size;};
    int getUsed(){return used;};
    //Operaciones
    void addElement(int xx);
    void addElement(int qtty,int *vec);
};
//Constructores
IntArr::IntArr(int sz){
  size = sz;
  used = 0;
  p = new int[size];
}
IntArr::IntArr(int sz,int qtty,int* vec){
  if(qtty>sz){
    sz = qtty;
  }
  size = sz;
  used = qtty;
  p = new int[size];
  p = vec;
}

//Destructor
IntArr::~IntArr(){
  delete []p;
}

//Visualizadores
void IntArr::prtArr(void) const{
  if(used == 0){
    cout<<endl<<"El array no tiene elementos."<<endl;
  }
  else{
    cout<<endl<<"Array: ";
    for(int i=0;i<used;i++){
      cout<<p[i]<<", ";
    }
    cout<<endl;
  }
}
void IntArr::prtArr(int cant){
  if(used == 0){
    cout<<endl<<"El array no tiene elementos."<<endl;
  }
  else{
    cout<<endl<<"Array: ";
    for(int i=0;i<cant;i++){
      cout<<p[i]<<", ";
    }
    cout<<endl;
  }
}

//Operaciones
double IntArr::getAvg(){
  double acum = 0;
  for(int i=0;i<used;i++){
    acum += p[i];
  }
  return (acum/used);
}
void IntArr::addElement(int xx){
  redimensionador(1);
  p[used] = xx;
  used++;
}
void IntArr::addElement(int qtty,int *vec){
  int j=0;
  redimensionador(qtty);
  for(int i=used;i<(used+qtty);i++){
    p[i] = vec[j];
    j++;
  }
  used += qtty;
}

//Verificador
void IntArr::redimensionador(int cant){
  if(cant+used>size){
    if(cant > 5){
      size += cant;
    }
    else{
      size += 5 + cant;
    }
  }
}

int main(int argc, char *argv[]){
  int v_aux[]= {0,5,10,15,20,25,30,35,40};
  IntArr A(10,sizeof(v_aux)/sizeof(int),v_aux);
  cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
  A.prtArr();
  A.addElement(77);
  cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
  A.prtArr();
  A.addElement(11);
  cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
  A.prtArr();
  A.addElement(8,v_aux);
  cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
  A.prtArr();
  PRESS_KEY;
}
    
asked by Emiliano Calvacho 17.04.2018 в 16:09
source

1 answer

1

In the program there are two problems, which can cause the program to stop working, both are related to memory management and pointers (Using pointers allows you to write in any memory location but you have to be sure that the memory is available):

In the constructor you are assigning the input pointer to the internal buffer, instead you have to copy the content

  IntArr::IntArr(int sz,int qtty,int* vec){
  if(qtty>sz){
    sz = qtty;
  }
  size = sz;
  used = qtty;
  p = new int[size];
  memcpy (p,vec,qtty*sizeof(int))
}

In the resize function you are only increasing the varialbe size, here you must also increase the size of the buffer

void IntArr::redimensionador(int cant){
   if(cant+used>size){
   if(cant > 5){
     size += cant;
   }
   else{
     size += 5 + cant;
  }
  int * temp = new int [size];
  memcpy (temp,p,used*sizeof(int)); // se copia el contenido del buffer
  delete [] p;// se borra el buffer anterior
  p = temp; //se asigna el nuevo buffer
  }
}
    
answered by 17.04.2018 / 18:46
source