Program with a GUI crashes when add .h and .cpp files
-
Hello! I'm working on a program that uses multiple classes and a GUI. The thing is that I want to make an extra file where all the global variables, whose values are constant and are used in all the program. This file I did it by creating a header where I define the type of each variable and in a .cpp, I give it the value to each variable. The problem is that before creating the .h and .cpp files the program ran without any problems, but when I add these files the program crashes.
#ifndef CONSTANTESRADARGUI_H #define CONSTANTESRADARGUI_H //VARIABLES GLOBALES CON UN VALOR CONSTANTE //se ponen todas las variables globales que se utilizan en todo el programa y su valor es constante //las variables se inicializan en constantesradargui.cpp //---------------------------- //parametros del radar extern float freqM; //frecuencia de muestreo, en m/s extern int nBI; //frecuencia de bearing increment BI, en Hz no se usa extern int nTRG; //frecuencia de trigger TRG, en Hz 2037 extern int rpmHM; //frecuencia del hearing mark HM, en rpm //---------------------------- //parametros de la matriz M extern int nfilas; //cantidad de filas de la matriz M extern int numeroColumna; //para modificar tamaño de la matriz (Lucas Pordomingo) //numeroColumna=1 para cantidad de columnas calculadas con parametros del radar //numeroColumna=2 para cantidad de columnas de 4096 //numeroColumna=3 para cantidad de columnas de 2096 //---------------------------- //parametros calculados a partir de los parametros del radar extern int VelGiro; //velocidad con la que gira el radar extern int ncolumnas; //numero de columnas de la matriz M, con los datos del radar extern int tamano_columna; //sirve para modificar distancia de graficado desde el centro del radar //tamano_columna = 1/(ncolumnas/8183) y ncolumnas= int(round((1/nTRG)*(freqM*1000000))), para el caso de usar una matriz M cantidad de columnas de acuerdo a los parametros del radar //tamano_columna = 2 y ncolumnas = 4096, para el caso de usar una matriz M con 4096 columnas //tamano_columna = 4 y ncolumnas = 2096, para el caso de usar una matriz M con 2096 columnas //---------------------------- //constantes que se utilizan en varios lugares del programa extern int niveles; //colores extern float area; //area del pixel que se dibuja extern float kmmilla; extern float rmillamuestra; extern float rfilaangulo; #endif // CONSTANTESRADARGUI_H#include<constantesradargui.h> #include<math.h> //---------------------------- //parametros del radar float freqM= 16.666; //frecuencia de muestreo, en m/s int nBI= 2049; //frecuencia de bearing increment BI, en Hz no se usa int nTRG= 2037; //frecuencia de trigger TRG, en Hz 2037 int rpmHM= 25; //frecuencia del hearing mark HM, en rpm //---------------------------- //parametros de la matriz M int nfilas= 4096; //cantidad de filas de la matriz M int numeroColumna= 1; //para modificar tamaño de la matriz (Lucas Pordomingo) //numeroColumna=1 para cantidad de columnas calculadas con parametros del radar //numeroColumna=2 para cantidad de columnas de 4096 //numeroColumna=3 para cantidad de columnas de 2096 //---------------------------- //parametros calculados a partir de los parametros del radar int VelGiro= (1/(rpmHM*0.02));; //velocidad con la que gira el radar int ncolumnas= int(round((1/nTRG)*(freqM*1000000))); //numero de columnas de la matriz M, con los datos del radar int tamano_columna =1/(ncolumnas/8183); //sirve para modificar distancia de graficado desde el centro del radar //tamano_columna = 1/(ncolumnas/8183) y ncolumnas= int(round((1/nTRG)*(freqM*1000000))), para el caso de usar una matriz M cantidad de columnas de acuerdo a los parametros del radar //tamano_columna = 2 y ncolumnas = 4096, para el caso de usar una matriz M con 4096 columnas //tamano_columna = 4 y ncolumnas = 2096, para el caso de usar una matriz M con 2096 columnas //---------------------------- //constantes que se utilizan en varios lugares del programa int niveles = 5; //colores float area = 0.1; //area del pixel que se dibuja float kmmilla=1.852; float rmillamuestra = (1 / (freqM * 1000000)) * (300000 / kmmilla); float rfilaangulo= nfilas/360;The errror is:
09:55:09: Starting /home/pc2/Descargas/build-Radar-Desktop-Debug/Radar...
09:55:10: /home/pc2/Descargas/build-Radar-Desktop-Debug/Radar crashed. -
Use a debugger and see where exactly it crashes.
-
Hi @Pordo,
There's probably other things, but this line crashes with a floating-point exception on my machine:
int tamano_columna =1/(ncolumnas/8183);The reason is that
ncolumnas(from the preceding line) evaluates to8182. And since all the values (both variables and literals) in that statement are integers, it evaluates like this:int tamano_columna =1/(ncolumnas/8183); =1/(8182/8183); =1/trunc(0.99987779543); =1/0; =exceptionYou can fix this particular exception by making either
ncolumnasor the8183literal afloatordouble; eg this does not crash for me:int tamano_columna =1/(ncolumnas/8183.0);But as @Christian-Ehrlicher said, the debugger is your friend :)
Cheers.
-
Hi @Pordo,
There's probably other things, but this line crashes with a floating-point exception on my machine:
int tamano_columna =1/(ncolumnas/8183);The reason is that
ncolumnas(from the preceding line) evaluates to8182. And since all the values (both variables and literals) in that statement are integers, it evaluates like this:int tamano_columna =1/(ncolumnas/8183); =1/(8182/8183); =1/trunc(0.99987779543); =1/0; =exceptionYou can fix this particular exception by making either
ncolumnasor the8183literal afloatordouble; eg this does not crash for me:int tamano_columna =1/(ncolumnas/8183.0);But as @Christian-Ehrlicher said, the debugger is your friend :)
Cheers.
@Paul-Colby said in Program with a GUI crashes when add .h and .cpp files:
int tamano_columna =1/(ncolumnas/8183.0);Good job for working through @Pordo's code to come to this! Indeed this will prevent the crash --- provided
ncolumnas != 8183.0! But @Pordo may have a deeper logic problem in this statement.Since the result is assigned to an
int, there is a "limited" set of possibilities for what the1 / somethingon the right-hand side will evaluate to anyway:- if
something == 0, it will crash/be undefined. - if
something >= 1, for any value ofsomethingthen1 / somethingwill evaluate to0as a (truncated) integer. - if and only if
0 < something < 1will it produce anyintgreat than0. Which means that forsomething, in the(ncolumnas/8183.0)expressionncolumnasneeds to be a lot less than8183.0if this is the intention. Is it? Else I wonder what OP is expecting the value oftamano_columnato be?
- if
-
Hello, I was reading their answers and tried to comment a section of the code that uses the variable tamano_columa. The code compiles, so I confirm that the problem was there.
The variable is used to maintain a distance ratio on a graphic. I resolved it changing the variable type to float.
Thanks to everyone for their answers, they were helpful. -
Hello, I was reading their answers and tried to comment a section of the code that uses the variable tamano_columa. The code compiles, so I confirm that the problem was there.
The variable is used to maintain a distance ratio on a graphic. I resolved it changing the variable type to float.
Thanks to everyone for their answers, they were helpful.@Pordo said in Program with a GUI crashes when add .h and .cpp files:
I resolved it changing the variable type to float.
That sounds a lot more useful :)
Just a comment: consider usingdoubleinstead offloatfor this, other constants and arithmetic. In C all floating point calculations are done indouble-precision anyway (at least I think/thought this was the case). Usingfloatinstead ofdoublethrows away precision for no good reason. The only time I would usefloats is to save space if I had millions of them....