01-18-2023 06:50 AM
Hello all,
why does the following code fails with "NON-FATAL RUN-TIME ERROR: Attempt to realloc uninitialized pointer" ? Even looking at the pointers with the debugger shows them as uninitialized instead of NULL... If I change "{0, Nb" to "{0, 0" it works !!!
#include <stdlib.h>
typedef struct sSpectro {
int Max, Nb;
double *B, *I, *MaxVolt, *MQ;
} tSpectro;
static int Nb=50000;
int main(void) {
tSpectro Data={0, Nb, NULL, NULL, NULL, NULL};
// Breakpoint on next line says that the pointers above are uninitialized. WHY?!?
// Data.B=Data.I=Data.MaxVolt=Data.MQ=NULL; // If I add this it works fine
for (;;) {
Data.I=realloc(Data.I, (Data.Nb=Nb)*sizeof(double));
// Runtime error on the above line with:
// NON-FATAL RUN-TIME ERROR: Attempt to realloc uninitialized pointer.
break;
}
free(Data.I);
}
01-24-2023 03:09 AM
Hi
You are trying to initialize the structure with a variable(static int Nb)
Jan
01-24-2023 03:43 AM
So ? That is valid C99, and even if not valid it should give a warning/error at compile time, not this weird 'uninitialized' runtime error.
01-24-2023
05:51 AM
- last edited on
03-24-2025
03:33 PM
by
Content Cleaner
CVI at this point is not *fully* C99, but the initialization indeed should work as expected. Here is the... most recent (or less older!) info I could find in this respect: ANSI C99 Extensions in LabWindows/CVI
I wonder whether using "designated" initializers would work:
typedef struct sSpectro {
int Max, Nb;
double *B, *I, *MaxVolt, *MQ;
} tSpectro;
static int Nb=50000;
tSpectro Data={.Max=0, .Nb=Nb, .*B=NULL, .*I=NULL, .*MaxVolt=NULL, .*MQ=NULL};