Problems with the malloc function

Asked

Viewed 41 times

2

I’m working with the library Fuzzy, she uses the operator "new", but I wanted to replace it with the function malloc. When replacing I got no response on the serial monitor. Can anyone tell me what the problem is?

Below is the code. The code with the operator new continues together but commented and just below is the equivalence with the function malloc.

#include <Fuzzy.h>
#include <SPI.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <stdlib.h>

#define ONE_WIRE_BUS 3

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

double temp1=0;


// Instantiating a Fuzzy object
//Fuzzy *fuzzy = new Fuzzy();
Fuzzy *fuzzy = (Fuzzy*)malloc(sizeof(Fuzzy()));

// Instantiating a FuzzyInput object
//FuzzyInput *temperature = new FuzzyInput(1);
FuzzyInput *temperature = (FuzzyInput*)malloc(sizeof(FuzzyInput(1)));
// Instantiating a FuzzySet object
//FuzzySet *cold = new FuzzySet(0, 20, 20, 25);
FuzzySet *cold = (FuzzySet*)malloc(sizeof(FuzzySet(0, 20, 20, 25)));


// Instantiating a FuzzySet object
//FuzzySet *good = new FuzzySet(23, 26, 26, 28);
FuzzySet *good = (FuzzySet*)malloc(sizeof(FuzzySet(23, 26, 26, 28)));

// Instantiating a FuzzySet object
//FuzzySet *hot = new FuzzySet(26, 40, 40, 40);
FuzzySet *hot = (FuzzySet*)malloc(sizeof(FuzzySet(26, 40, 40, 40)));



void setup() {
// Set the Serial output
Serial.begin(9600);
// Set a random seed
randomSeed(analogRead(0));

// Including the FuzzySet into FuzzyInput
temperature->addFuzzySet(cold);
// Including the FuzzySet into FuzzyInput
temperature->addFuzzySet(good);
// Including the FuzzySet into FuzzyInput
temperature->addFuzzySet(hot);
// Including the FuzzyInput into Fuzzy
fuzzy->addFuzzyInput(temperature);

// Instantiating a FuzzyOutput objects
//FuzzyOutput *decisao = new FuzzyOutput(1);
FuzzyOutput *decisao = (FuzzyOutput*)malloc(sizeof(FuzzyOutput(1)));
// Instantiating a FuzzySet object
//FuzzySet *aumenta = new FuzzySet(0, 2, 2, 4);
FuzzySet *aumenta = (FuzzySet*)malloc(sizeof(FuzzySet(0, 2, 2, 4)));
// Including the FuzzySet into FuzzyOutput
decisao->addFuzzySet(aumenta);
// Instantiating a FuzzySet object
//FuzzySet *ok = new FuzzySet(2, 6, 8, 10);
FuzzySet *ok = (FuzzySet*)malloc(sizeof(FuzzySet(2, 6, 8, 10)));
// Including the FuzzySet into FuzzyOutput
decisao->addFuzzySet(ok);
// Instantiating a FuzzySet object
//FuzzySet *diminui = new FuzzySet(8, 10, 10, 12);
FuzzySet *diminui = (FuzzySet*)malloc(sizeof(FuzzySet(8, 10, 10, 12)));
// Including the FuzzySet into FuzzyOutput
decisao->addFuzzySet(diminui);
// Including the FuzzyOutput into Fuzzy
 fuzzy->addFuzzyOutput(decisao);

// Building FuzzyRule "IF temperature = cold THEN decisao = aumenta"
// Instantiating a FuzzyRuleAntecedent objects
//FuzzyRuleAntecedent *ifTemperatureCold = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent *ifTemperatureCold = 
(FuzzyRuleAntecedent*)malloc(sizeof(FuzzyRuleAntecedent()));
// Creating a FuzzyRuleAntecedent with just a single FuzzySet
ifTemperatureCold->joinSingle(cold);
// Instantiating a FuzzyRuleConsequent objects
//FuzzyRuleConsequent *thenDecisaoAumenta = new FuzzyRuleConsequent();
FuzzyRuleConsequent *thenDecisaoAumenta = 
(FuzzyRuleConsequent*)malloc(sizeof(FuzzyRuleConsequent()));
// Including a FuzzySet to this FuzzyRuleConsequent
thenDecisaoAumenta->addOutput(aumenta);
// Instantiating a FuzzyRule objects
//FuzzyRule *fuzzyRule01 = new FuzzyRule(1, ifTemperatureCold, 
thenDecisaoAumenta);
FuzzyRule *fuzzyRule01 = (FuzzyRule*)malloc(sizeof(FuzzyRule(1, 
ifTemperatureCold, thenDecisaoAumenta)));
// Including the FuzzyRule into Fuzzy
fuzzy->addFuzzyRule(fuzzyRule01);

// Building FuzzyRule "IF temperature = good THEN decisao = ok"
// Instantiating a FuzzyRuleAntecedent objects
//FuzzyRuleAntecedent *ifTemperatureGood = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent *ifTemperatureGood = 
(FuzzyRuleAntecedent*)malloc(sizeof(FuzzyRuleAntecedent()));
// Creating a FuzzyRuleAntecedent with just a single FuzzySet
ifTemperatureGood->joinSingle(good);
// Instantiating a FuzzyRuleConsequent objects
//FuzzyRuleConsequent *thenDecisaoOk = new FuzzyRuleConsequent();
FuzzyRuleConsequent *thenDecisaoOk = 
(FuzzyRuleConsequent*)malloc(sizeof(FuzzyRuleConsequent()));
// Including a FuzzySet to this FuzzyRuleConsequent
thenDecisaoOk->addOutput(ok);
// Instantiating a FuzzyRule objects
//FuzzyRule *fuzzyRule02 = new FuzzyRule(2, ifTemperatureGood, thenDecisaoOk);
FuzzyRule *fuzzyRule02 = (FuzzyRule*)malloc(sizeof(FuzzyRule(2, ifTemperatureGood, thenDecisaoOk)));
// Including the FuzzyRule into Fuzzy
fuzzy->addFuzzyRule(fuzzyRule02);

 // Building FuzzyRule "IF temperature = hot THEN decisao = diminui"
 // Instantiating a FuzzyRuleAntecedent objects
 //FuzzyRuleAntecedent *ifTemperatureHot = new FuzzyRuleAntecedent();
 FuzzyRuleAntecedent *ifTemperatureHot = (FuzzyRuleAntecedent*)malloc(sizeof(FuzzyRuleAntecedent()));
 // Creating a FuzzyRuleAntecedent with just a single FuzzySet
  ifTemperatureHot->joinSingle(hot);
 // Instantiating a FuzzyRuleConsequent objects
 //FuzzyRuleConsequent *thenDecisaoDiminui = new FuzzyRuleConsequent();
 FuzzyRuleConsequent *thenDecisaoDiminui = (FuzzyRuleConsequent*)malloc(sizeof(FuzzyRuleConsequent()));
 // Including a FuzzySet to this FuzzyRuleConsequent
 thenDecisaoDiminui->addOutput(diminui);
 // Instantiating a FuzzyRule objects
 //FuzzyRule *fuzzyRule03 = new FuzzyRule(3, ifTemperatureHot, thenDecisaoDiminui);
 FuzzyRule *fuzzyRule03 = (FuzzyRule*)malloc(sizeof(FuzzyRule(3, ifTemperatureHot, thenDecisaoDiminui)));
 // Including the FuzzyRule into Fuzzy
 fuzzy->addFuzzyRule(fuzzyRule03);
 }

  void loop() {
    sensors.requestTemperatures();
    temp1 = sensors.getTempCByIndex(0);

    fuzzy->setInput(1, temp1);

    // Running the Fuzzification
    fuzzy->fuzzify();

    float pertinenceOfHot = hot->getPertinence();
    float pertinenceOfGood = good->getPertinence();
    float pertinenceOfCold = cold->getPertinence();

    bool wasTheRulleFired1 = fuzzy->isFiredRule(1);
    bool wasTheRulleFired2 = fuzzy->isFiredRule(2);
    bool wasTheRulleFired3 = fuzzy->isFiredRule(3);

    // Running the Defuzzification
    float output = fuzzy->defuzzify(1);

    // Printing something
    Serial.println("Result: ");
    Serial.print("\t\t\tTemperature: ");
    Serial.println(temp1);
    Serial.print("\t\t\tDecision: ");
    if (output > 9){
    Serial.println("Diminuir a temperatura");
    }
    else if (output < 3){
    Serial.println("Aumentar a temperatura");
    }
    else{
    Serial.println("A temperatura está ok");
    }
    Serial.print("\t\t\tOutput: ");
    Serial.println(output);
    Serial.print("\t\t\tTemperature: Cold-> ");
    Serial.print(cold->getPertinence());
    Serial.print(", Good-> ");
    Serial.print(good->getPertinence());
    Serial.print(", Hot-> ");
    Serial.println(hot->getPertinence());

    Serial.print("\t\t\tRegra: 1-> ");
    Serial.print(fuzzy->isFiredRule(1));
    Serial.print(", 2-> ");
    Serial.print(fuzzy->isFiredRule(2));
    Serial.print(", 3-> ");
    Serial.println(fuzzy->isFiredRule(3));
    // wait 2 seconds
    delay(1000);
    }

1 answer

1

In the general case you cannot replace "new" with "malloc" because when you use "new" the class constructor (fuzzy in this case) is also executed...

If you call "malloc" instead of "new" then you should too explicitly execute code equivalent to class constructor code.

  • And how do I make an equivalent class builder code?

  • you have to see the constructor code of the class...in case, you wanted to take the constructor code of the Fuzzy class, and create a fuzzy_init(Fuzzy* fuzzyPtr) function...actually, unless you can’t use C++ (for example, you can only use C) i think that what you did (replace "new" by "malloc") does not make much sense, it is a waste of time

  • I cannot use C++, hence the amendment

  • I still don’t understand how I should make the code equivalent to the class constructor code...

  • 1

    you accurate get access to the Fuzzy function constructor code...hence you turn this C++ code into a C function, which you will call right after doing the malloc of your Fuzzy object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.