Search code examples
c++memory-managementrealloc

Reallocating memory correctly in c++


#include <iostream>
#include <stdlib.h>

class circuitTypes{

protected:
    static int size;
    circuitTypes **Matrix;
    int input1,input2;
    int output1, output2;

public:

    circuitTypes() {};
    static int getSize() { return size; };
    static void upSize() { size++; };
    void ItemRegistry();
    virtual void setTruthTable()=0;
    void setInputAndCalculateOutput(int a, int b);
    int *getOutput();
};

int circuitTypes::size=0;

int *circuitTypes::getOutput(){
    int Output[2];
    Output[0]=output1;
    Output[1]=output2;
    return Output;
}
void circuitTypes::ItemRegistry(){

    circuitTypes::upSize();
    int circuitSize=circuitTypes::getSize();

    if(circuitSize==1)
        Matrix=(circuitTypes **)malloc(circuitSize*sizeof(circuitTypes *));
    else
        Matrix=(circuitTypes **)realloc(Matrix,circuitSize*sizeof(circuitTypes *));
    if(Matrix==0){
        std::cout <<"No available memory \n";
        exit(1);
    }
    Matrix[circuitSize-1]=this;
}


void circuitTypes::setInputAndCalculateOutput(int a, int b){
    input1=a;
    input2=b;
    setTruthTable();
}


class TypeA : private circuitTypes{

public:
    TypeA() { ItemRegistry(); };
    void setTruthTable();
};

void TypeA::setTruthTable(){
    if (input1==0){
        if (input2==0){
            output1=0;
            output2=0;
        }
        else{
            output1=0;
            output2=1;
        }
    }
    else{
        if (input2==0){
            output1=0;
            output2=1;
        }
        else{
            output1=1;
            output2=1;
        }
    }
}


class TypeB : private circuitTypes{
public:
    TypeB() { ItemRegistry(); };
    void setTruthTable();
};

void TypeB::setTruthTable(){
    if (input1==0){
        if (input2==0){
            output1=0;
            output2=0;
        }
        else{
            output1=0;
            output2=1;
        }
    }
    else{
        if (input2==0){
            output1=1;
            output2=1;
        }
        else{
            output1=0;
            output2=1;
        }
    }
}

void circuit (circuitTypes **Example, int a, int b){
    std::cout << "NIKKK";
    Example[0]->setInputAndCalculateOutput(a,b);
    int Size=Example[0]->getSize();
    for (int i=1;i<Size;i++){

        Example[i]->setInputAndCalculateOutput(Example[i-1]->getOutput()[0],Example[i-1]->getOutput()[1]);

    }
    std::cout << "For input a= " << a << " and b= " << b << " the result is c= " << Example[Size-1]->getOutput()[0] << " and d=" << Example[Size-1]->getOutput()[1] << "\n";
}


int main (){

    circuitTypes **Example;
    TypeA A1,A2,A3;
    TypeB B1,B2,B3;
    for (int i=0;i<2;i++){

        for (int j=0;j<2;j++){
            circuit (Example,i,j);
        }

    }

}

I 'm sorry for the size of the code, but as I have no idea where the problem is I decided to post the whole programm. The .cpp file is compiled without any problem in Ubuntu 11.10 (with g++) but when I try to execute the a.out file I get this error:

* glibc detected ./a.out: realloc(): invalid pointer: 0x003c2ff4 **

======= Backtrace: =========

/lib/i386-linux-gnu/libc.so.6(+0x721a2)[0x2b71a2]

/lib/i386-linux-gnu/libc.so.6(realloc+0x2a5)[0x2bb245]

./a.out[0x8048835]

./a.out[0x8048ca9]

./a.out[0x8048b2a]

/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x25e113]

./a.out[0x80486c1]

======= Memory map: ========

00110000-0012c000 r-xp 00000000 08:06 1311674 /lib/i386-linux-gnu/libgcc_s.so.1

0012c000-0012d000 r--p 0001b000 08:06 1311674 /lib/i386-linux-gnu/libgcc_s.so.1

etc.

Can anyone help me?


Solution

    1. It is bad to set Matrix = realloc since you will never be able to free it in case realloc fails. But you are exiting immediately anyway so the code won't leak but it is still bad practice.
    2. You are just declaring circuitTypes **Example and using it in circuit(Example, i, j) without allocating memory to it. You probably want to use Matrix for that.
    3. This is a pure guess: the way your code is structured makes me think that you want to make Matrix static and use it instead of Example.