简体   繁体   English

在C ++中正确重新分配内存

[英]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: 在Ubuntu 11.10(使用g ++)中编译.cpp文件时没有任何问题,但是当我尝试执行a.out文件时,出现此错误:

* glibc detected *检测到glibc ./a.out: realloc(): invalid pointer: 0x003c2ff4 * * ./a.out:realloc():无效的指针:0x003c2ff4 * *

======= Backtrace: ========= =======回溯:=========

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

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

./a.out[0x8048835] ./a.out[0x8048835]

./a.out[0x8048ca9] ./a.out[0x8048ca9]

./a.out[0x8048b2a] ./a.out[0x8048b2a]

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

./a.out[0x80486c1] ./a.out[0x80486c1]

======= Memory map: ======== =======内存映射:========

00110000-0012c000 r-xp 00000000 08:06 1311674 /lib/i386-linux-gnu/libgcc_s.so.1 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 0012c000-0012d000 r--p 0001b000 08:06 1311674 /lib/i386-linux-gnu/libgcc_s.so.1

etc. 等等

Can anyone help me? 谁能帮我?

   if(circuitSize==1)
       Matrix=(circuitTypes **)malloc(circuitSize*sizeof(circuitTypes *));
   else
       Matrix=(circuitTypes **)realloc(Matrix,circuitSize*sizeof(circuitTypes *));

I don't understand the if condition here. 我不明白这里的if条件。 Surely it should be if (Matrix == 0) ? 当然应该是if (Matrix == 0) Otherwise you run the risk of trying to realloc something that you have never malloc -ed in the first place. 否则,你运行的尝试风险realloc你从来没有的东西malloc在首位-ed。

But I agree with @JerryCoffin that you should throw it out and use a std::vector . 但是我同意@JerryCoffin的观点,您应该将其丢弃并使用std::vector

  1. It is bad to set Matrix = realloc since you will never be able to free it in case realloc fails. 设置Matrix = realloc很不好,因为如果realloc失败,您将永远无法释放它。 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. 您只是声明circuitTypes ** Example,并在circuit(Example,i,j)中使用它,而没有为其分配内存。 You probably want to use Matrix for that. 您可能要为此使用Matrix。
  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. 这纯粹是猜测:代码的结构方式使我认为您想使Matrix静态化并使用它而不是Example。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM