Problem executing c ++ code in Netbeans "Debugger error: During startup program exited with code 0xc0000139."

1

I am programming a simple code in C ++ with NetBeans. The code compiles me well in NetBeans and in other environments (Dev C ++) it works. However, when running in Netbeans I get the following error:

  

RUN FAILED (exit value -1.073.741.511, total time: 47ms)

Then I use the debugger and I get the following message:

  

Debugger error: During startup program exited with code 0xc0000139.

And it does not finish executing.

The code is:

#include <iostream>
#include <cstring>
using namespace std;

class cadena {
    public:
    cadena();        // Constructor por defecto
    cadena(const char *c); // Constructor desde cadena c
    cadena(int n);   // Constructor de cadena de n caracteres
    cadena(const cadena &);   // Constructor copia
    ~cadena();       // Destructor

    void Asignar(const char *dest);
    char *Leer(char *c);
    private:
    char *cad;       // Puntero a char: cadena de caracteres
};

cadena::cadena() : cad(NULL) {}

cadena::cadena(const char *c) {
    cad = new char[strlen(c)+1];// Reserva memoria para cadena
    strcpy(cad, c);             // Almacena la cadena
}

cadena::cadena(int n) {
    cad = new char[n+1]; // Reserva memoria para n caracteres
    cad[0] = 0;          // Cadena vacía
}

cadena::cadena(const cadena &Cad) {
    // Reservamos memoria para la nueva y la almacenamos
    cad = new char[strlen(Cad.cad)+1];
    // Reserva memoria para cadena
    strcpy(cad, Cad.cad);             // Almacena la cadena
}

cadena::~cadena() {
    delete[] cad;        // Libera la memoria reservada a cad
}

void cadena::Asignar(const char *dest) {
    // Eliminamos la cadena actual:
    delete[] cad;
    // Reservamos memoria para la nueva y la almacenamos
    cad = new char[strlen(dest)+1];
    // Reserva memoria para la cadena
    strcpy(cad, dest);              // Almacena la cadena
}

char *cadena::Leer(char *c) {
    strcpy(c, cad);
    return c;
}

int main() {
    cadena Cadena1("Cadena de prueba");
    cadena Cadena2(Cadena1);   // Cadena2 es copia de Cadena1
    cadena *Cadena3;           // Cadena3 es un puntero
    char c[256];

    // Modificamos Cadena1:
    Cadena1.Asignar("Otra cadena diferente");
    // Creamos Cadena3:
    Cadena3 = new cadena("Cadena de prueba nº 3");

    // Ver resultados
    cout << "Cadena 1: " << Cadena1.Leer(c) << endl;
    cout << "Cadena 2: " << Cadena2.Leer(c) << endl;
    cout << "Cadena 3: " << Cadena3->Leer(c) << endl;

    delete Cadena3;  // Destruir Cadena3.
    // Cadena1 y Cadena2 se destruyen automáticamente

    return 0;
}
    
asked by mjcmsp 12.07.2016 в 09:50
source

1 answer

2

Your program itself does not have errors when compiling / running, so your error may be due to poor configuration of the netbeans project. In the absence of more information on your part the answer to your problem ends here.

What does happen is that your class cadena is dangerous by definition.

Imagine doing the following:

cadena cad1;
cadena cad2(cad1);
char c[255];

std::cout << cad1.Leer(c) << std::endl;
std::cout << cad2.Leer(c) << std::endl;

How will the function behave?

Well, if we look at the default constructor implementation:

cadena::cadena() : cad(NULL) {}

We will have the pointer cad1.cad points to 0 (note that pointing to 0 does not imply that *cad1.cad==0 ).

Then we call the constructor copy to build cad2 :

cadena::cadena(const cadena &Cad) {
    // Reservamos memoria para la nueva y la almacenamos
    cad = new char[strlen(Cad.cad)+1];
    // Reserva memoria para cadena
    strcpy(cad, Cad.cad);             // Almacena la cadena
}

And here the problems begin. What returns strlen(0) ? I already advance you that it does not have to return anything good, that supposing that the program does not crack. At the end you will make a memory reserve of undetermined size and you will try to make a copy such that strcpy(cad,0) , which does not look too good either.

To finish off the task, then imagine that we continue executing and we reach the Leer method:

char *cadena::Leer(char *c) {
    strcpy(c, cad);
    return c;
}

std::cout << cad1.Leer(c) << std::endl;

And we repeat the problems again: strcpy(c,0) and std::cout << (char*)0 << std::endl; .

You can get an idea that the problems caused by the default constructor are varied. My proposal is that you declare the default constructor as private and do not add it to the implementation, so that outside the class it will be inaccessible (because it is private) and within the class it can not be invoked either (since it has no implementation).

Greetings.

    
answered by 12.07.2016 в 10:43