Access violation new c++ struct

0

I have this struct:

struct event_ {

    bool is_crossover = false;
    bool is_birth = false;
    bool is_repetitive = false;

    int eID = 0;

    bool inicio_fin = false; 
    fecha inicio_fecha;
    fecha fin_fecha;

    locacion inicio_l;
    string eLatitud_i = 0; 
    string eLongitud_i = 0;

    locacion fin_l;
    string eLatitud_f = 0;
    string eLongitud_f = 0;

    personaje_info personajes_evento; //This is a class
    int cantidad_personajes = 0;

    string nombre;
    string descripcion;
    string tipo_evento;

    event_ *sig, *ant;
};

And then, when I call the function:

event_ *n = new event_;

it sends me an Access Violation Error:

Exception thrown at 0x0F69F6E0 (ucrtbased.dll) in Auxiliar Libros.exe: 0xC0000005: Access violation reading location 0x00000000.

Anyone knows why is this happening? As additional information, I ran a Code Metrics Analysis, and before this, the program worked perfectly fine. And also it tells me about exceptions, what should I do?

c++
struct
visual-studio-2017
new-operator
access-violation
asked on Stack Overflow Mar 9, 2019 by shiosaku • edited Mar 9, 2019 by Paul Rooney

2 Answers

1

This code

string eLongitud_f = 0;

calls the string constructor with a NULL pointer (0 is another way of writing the NULL pointer), resulting in your access validation error.

What do you think that code is doing? Obviously 0 is an integer not a string. Did you mean this?

string eLongitud_f = "0";

Or did you mean this?

string eLongitud_f = "";

Maybe you even meant this

double eLongitud_f = 0.0;

You can also just have this

string eLongitud_f;

which is the same as the second alternative above. All these are possible, it's hard to know which you really want, but the fundamental problem is that you are have a string variable and you are trying to give it a value which is not a string.

answered on Stack Overflow Mar 9, 2019 by john
0

To solve your problem, I think that the best thing to do is to reduce your code and try some combinaison.

First, you must try a little struct with only one bool variable to see if your new function is correct

struct event_
    {
    bool is_crossover = false;
    };

event_ *n = new event_;

If your program continue to crash, your error is there, in new().

Else you can try then to reduce your structure removing what you think is correct.

Personnaly, I think that all your bool, int and event_ declaration are correct, so I remove them. I think that similar object declaraction can also be removed and I remove them.

I have following structure:

struct event_
    {
    fecha fin_fecha;
    locacion inicio_l;
    string eLatitud_i = 0; 
    personaje_info personajes_evento;
    };

What happens when you build and run this code ?

If you program has stopped to crash, the error is in removed code ?

Else, one (or more) declaration's line of this new structure is incorrect.

If changing your struct has too much impact in your code, your create a similar structure (other name not yet used) and you test it.

Please, can you try ? I think that you will find very quickly solve the problem yourself !

There are too much variables in your first code that can produce your crash ?

answered on Stack Overflow Mar 9, 2019 by schlebe

User contributions licensed under CC BY-SA 3.0