-4

I'm a novice to use c++ and morely qt. I've developed a multi-class software to calculate easter date. Now i'd like to write it to a label on button click, but i get a -i think- segmentation fault. My code is this -just a function, but please, ask if you want to see other parts-:

    void MainWindow::on_btn_calcola_clicked()
    {
        Easter *e;
        int anno = ui->text_anno->text().split(" ")[0].toInt();
        int *date = e->dataPasqua(anno);
        int giorno = date[0];
        int mese = date[1];
        QString d = QString::number(giorno);
        QString m = QString::number(mese);
        QString dataCompleta = d+"/"+m;
        ui->lbl_result->setText(dataCompleta);
    }

What's wrong? Thankyou so much!

lucad93
  • 45
  • 6
  • 2
    As the answer points out you did not initialize `e`. Does `e` need to be a pointer? I believe the answer is no. `e` should not be a pointer. You don't want to dynamically allocate memory for `e` and then have to free it in the same function. Does `e` do some internal calculation based on the current date or does it require some other input that you are not giving it? My other advice is to use better naming of your variables. Avoid 1 letter names for variables unless the variable is an index variable (like one you would use for a loop). – drescherjm Jul 22 '19 at 12:59
  • 1
    I guess you use a `QLineEdit` for the user input; better use `QDateEdit`. This will also ensure only 'valid' dates are entered. – Zaiborg Jul 22 '19 at 13:46

2 Answers2

1

Variable "e" is declared, but not initialized. Possible solution is to use "make_unique" or simple new operator, like: e = new Easter();

  • 1
    `Easter e;` might also do the trick; need to replace `e->` with `e.`. – Zaiborg Jul 22 '19 at 13:48
  • Probably the only reason you think you need a pointer is because you used `e->` instead of `e.` – drescherjm Jul 22 '19 at 14:00
  • I'm sorry, here you can find the whole project: https://gitlab.com/lucad93/easter.git – lucad93 Jul 22 '19 at 14:26
  • 1
    You have a better chance at getting help if you put the required code in the question itself. Remember that the main purpose of a `StackOverflow` question is to help future readers with the same problem. – drescherjm Jul 22 '19 at 14:53
  • 1
    `return data;` from `dataPasqua()` is a bug. You can not rerturn a pointer to a local variable that goes out of scope. That is Undefined Behavior. https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope/6445794#6445794 – drescherjm Jul 22 '19 at 14:54
  • So, how can i fix it? What can i return? – lucad93 Jul 22 '19 at 14:59
  • 1
    You probably should return a `QString` or a `QDate`. But then there is a question why `Easter` is a class at all since it is just a single function with no member variables. – drescherjm Jul 22 '19 at 15:07
1

Tried a declaration like below for Easter object. (Is better to avoid pointer when is not needed)

Easter e = Easter();

And in a second time, try to return a QList instead of a pointer in your function dataPasqua()

Is the two potential issues, the rest seems to be ok.

Yoann
  • 42
  • 1
  • 8