Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why is this code producing a "core dumped error"? It gives the right result, but indicates that a way I'm deallocating the allocated memory is wrong and I don't know why.

What I have tried:

C++
<pre>#include <iostream>
#include <stdexcept>

template<typename Tip>
class Lista{
    public:
    Lista(){};
    virtual ~Lista(){};
    virtual int returnNumberOfElements()const=0;
    virtual void removeElement()=0;
    virtual void addInFrontOfIndex(const Tip &el)=0;
};

template<typename Tip>
class NizLista:public Lista<int>{
   Tip **niz;
   int numberOfElements,capacity,index;
   public:
   NizLista(){
       capacity=100;
       numberOfElements=0;
       index=0;
       niz=new Tip*[capacity]{};
   }

   ~NizLista(){ for(int i=0;i<capacity;i++) delete niz[i];
   delete[] niz; 
   }
   int returnNumberOfElements()const;
   void removeElement();
   void addInFrontOfIndex(const Tip &el);
};



template<typename Tip>
int NizLista<Tip>::returnNumberOfElements()const{return numberOfElements;}

template<typename Tip>
void NizLista<Tip>::removeElement(){
    if(numberOfElements==0) throw std::logic_error("List empty");
    delete niz[index];
    for(int i=index;i<numberOfElements-1;i++)
        niz[i]=niz[i+1];
    numberOfElements-=1;
}

template<typename Tip>
void NizLista<Tip>::addInFrontOfIndex(const Tip &el){
    if(numberOfElements!=0){
        for(int i=numberOfElements;i>index;i--)
        niz[i]=niz[i-1];
    }
    niz[index]=new Tip(el);
    numberOfElements++;
}



int main() {
NizLista<int> niz;
for (int i(1); i<=5; i++)
	niz.addInFrontOfIndex(i);
{
    NizLista<int> niz2(niz);
    NizLista<int> niz3;
    niz3=niz;
    niz.removeElement();
    std::cout << niz2.returnNumberOfElements();
    std::cout << " " << niz3.returnNumberOfElements() << " ";
}
std::cout << niz.returnNumberOfElements();
    return 0;
}
Posted
Updated 11-Nov-20 20:18pm
v2

It appears that you're putting integers into the array but then treating them as pointers in the first line of your destructor. I think you want
niz=new Tip*[capacity]{}; // not Tip**

EDIT: Now I notice that you're passing a pointer to each integer when you add it to the array. But the integer comes from the loop variable i, which will cause problems when you try to delete it, because it was an address on the stack.

The convention, when writing a container, is for it to hold instances of <T>, not <T*>. When the container is meant to hold pointers, then the code using the template should specify this explicitly.
 
Share this answer
 
v9
Your code has several issues:
Quote:
for(int i=0;i<capacity;i++) delete niz[i];

  • Your class is deleting objects whose lifetime is not controlled by it.


Quote:
NizLista<int> niz2(niz);
Quote:
niz3=niz;
  • Your class (badly) needs non-trivial copy constructor and assignment operator. The default ones are not appropriate (multiple delete on the same pointer).


  • The index member variable has no usage (and it is harmful) use consistently numberOfElements, instead.


  • You are not checking if the container numberOfElements are at capacity limit, before actually adding a new item.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900