Problem when printing string array from a pointer and repeating the main C ++ method

0

I have an "Element" class that has a "items" pointer, of type string (from the string.h library), responsible for saving a reference to an array of items in the Element. Everything works fine if I do not run the app within a while. However, while using the while, the program stops working if I try to print the items of the element I created earlier. To be clearer, what I did was exactly:

1 - Modify the element

2 - Show item items

When displaying element items, several strange characters appear on the console screen and the program stops working, without items even being printed on the screen.

Classes and application codes follow:

Class Element:

#include<iostream>
#include<string.h>

using namespace std;

class Elemento{

private:

    string *itens;
    int size;

public:

    Elemento(string itens[], int size){
        this->itens = itens;
        this->size = size;
    }

    void showItens(){
        for(int i = 0; i < this->size; i++){
            cout<<endl<<itens[i]<<endl;
        }
    }
};

Application code:

#include<iostream>
#include "Elemento.cpp"
#include<string.h>

Elemento *el;

bool loop = true;

int main(){
    while(loop){

        int num;

        cout<<endl<<"1 - Modificar o elemento"<<endl;
        cout<<"2 - Mostrar os itens do elemento"<<endl<<endl;
        cin>>num;

        cin.sync();

        switch(num){
            case 1:{
                int num;

                cout<<"Digite o numero de itens: "<<endl;
                cin>>num;

                string itens[num];

                for(int i = 0; i < num; i++){
                    cout<<"Digite o item: "<<i+1<<": "<<endl;
                    cin>>itens[i];
                    cin.sync();
                }

                el = new Elemento(itens,num);

                el->showItens();
                break;
            }
            case 2:{
                if(el != NULL){
                    el->showItens();
                }
                break;
            }
            case 3:{
                loop = false;
                break;
            }
        }
    }
}

Could anyone tell me the cause of this problem?

// Update

I have refactored the code using malloc. They looked like this:

Class Element:

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

using namespace std;

class Elemento{
    private:

        string *itens;
        int size;

    public:

        Elemento(string itens[], int size){
            this->itens = (string*)malloc(size * sizeof(string));
            this->itens = itens;
            this->size = size;
        }

        void showItens(){
            for(int i = 0; i < this->size; i++){
                cout<<endl<<itens[i]<<endl;
            }
        }
};

App class:

#include<iostream>
#include "Elemento.cpp"
#include<string.h>
#include<stdlib.h>

Elemento *el;

bool loop = true;

int main(){
    while(loop){

        int num;

        cout<<endl<<"1 - Modificar o elemento"<<endl;
        cout<<"2 - Mostrar os itens do elemento"<<endl<<endl;
        cin>>num;

        cin.sync();

        switch(num){
            case 1:{
                int num;

                cout<<"Digite o numero de itens: "<<endl;
                cin>>num;

                string *itens;
                itens  = (string *)malloc(num * sizeof(string));

                for(int i = 0; i < num; i++){
                    cout<<"Digite o item: "<<i+1<<": "<<endl;
                    cin>>itens[i];
                    cin.sync();
                }

                el = (Elemento*)malloc(sizeof(Elemento)) ;
                el = new Elemento(itens,num);

                el->showItens();
                break;
            }
            case 2:{
                if(el != NULL){
                    el->showItens();
                }
                break;
            }
            case 3:{
                loop = false;
                break;
            }
        }
    }

}

I do not know if it is necessary to use malloc inside the constructor of the Element class. I believe the problem in my code is to iterate over the 'items' pointer. I saw in some sources that it was possible to iterate over it in the same way as an array, in the form 'items [index]'. However, my program still stops working in this part of the program.

    
asked by anonymous 27.09.2016 / 22:53

1 answer

1

There are several problems in your code, I will concentrate on only one of them.

Here

el = new Elemento(itens,num);

You are creating an anonymous object of type Element, and in the constructor of the Element object you initialize the "items" pointer to reference the array of "items" strings that was created in the "case" 1 of the "switch" / p>

Problem: As soon as the execution exits case 1, the array of strings is deallocated, it dies, so the object created above now has an invalid "items" pointer, pointing to an object that no longer exists. Also, most likely the memory of the array of strings that was destroyed in the output of case 1 will be reused and overwritten, getting corrupted (in the sense that it will no longer be an array of strings, other things will be written on top). Because of this, if soon after you choose option 2 to print the contents of the Element object most likely the program will behave abnormally, it will probably be canceled by mistake.

    
29.09.2016 / 01:38