0

I have this class for save receipt :

class Recept { 
    int Ingr_size;
    char *Name;
    char *Type;
    char *Recipe;
    struct Ingridient {
        char *aName;
        float Mas;

    } *List_ingr;

when i trying to save ingredient name or mass by using this function:

void Recept::setIngr(const char * p, float mass) {
    struct Ingridient * temp = new struct Ingridient[Ingr_size + 1];
    if (Ingr_size) {
        for (int i = 0; i < Ingr_size; i++)
            temp[i] = List_ingr[i];
        delete List_ingr;
        List_ingr = temp;
    }
    List_ingr[Ingr_size].aName = new char[strlen(p) + 1];
    strcpy(List_ingr[Ingr_size].aName, p);
    List_ingr[Ingr_size].Mas = mass;
}

I get an error that "Unable to reach aName memory" & "Unable to reach Mas memory".

I can't find the problem where or why.

Thank you.

Moe diev
  • 113
  • 1
  • 7
  • 1
    Why aren't you using `std::vector` or `std::string`? – trojanfoe May 17 '16 at 08:30
  • Also keyword `struct` is not needed when instantiate in C++. And you can use `std::list` or `std::vector` to manage your list of Ingridient, instead of dealing with raw array – Garf365 May 17 '16 at 08:32

1 Answers1

0

I get an error that "Unable to reach aName memory" & "Unable to reach Mas memory".

this is a runtime exception, you have not initiaized your class variables, you need at least a constructor which will do basic default initailizations:

Recept() : Ingr_size(0), List_ingr(nullptr){}

also, in case List_ingr is nullptr you should not try iterating it inside setIngr.

[edit]

If you have constructor as above in your class (which is not in the question), then you have other errors, see comments below:

void Recept::setIngr(const char * p, float mass) {
    struct Ingridient * temp = new struct Ingridient[Ingr_size + 1];
    if (Ingr_size) {
        for (int i = 0; i < Ingr_size; i++)
            temp[i] = List_ingr[i];
        delete List_ingr;
        List_ingr = temp;
    }
    else {
        // !!! in case of Ingr_size you still need to set List_ingr,
        List_ingr = temp;
    }
    List_ingr[Ingr_size].aName = new char[strlen(p) + 1];
    strcpy(List_ingr[Ingr_size].aName, p);
    List_ingr[Ingr_size].Mas = mass;

    // !!! You need to increment size
    Ingr_size++;
}
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • 1
    A destructor wouldn't hurt either if `new` is being used to allocate resource. – trojanfoe May 17 '16 at 08:49
  • And in this case, is more secure to use a `std::list` to manage the ingredient list – Garf365 May 17 '16 at 08:49
  • @trojanfoe also, a copy assignment and a copy constructor (see http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Garf365 May 17 '16 at 08:50
  • @Garf365 agree, this class requires all the constructors + destructor – marcinj May 17 '16 at 08:56
  • Well, my answer fixes Undefined Behaviour in this code - which was causing crashes - but by no means it makes this code work as it is supposed to, that requires more work. At leas implementation of RAII. – marcinj May 17 '16 at 08:57
  • but i'm using instructor in my class: 'Recip() // Instructor { Name = 0; Type = 0; List_ingr= 0; Recipe = 0; Ingr_size= 0; };' – Moe diev May 17 '16 at 09:00