Program crash after trying to access and write to structure member

-1

Working in Visual Studio in C and trying to do fft of some samples. When I attempt writing some value to member of struct my program crash and I get error access violation writing location 0x00000000.

First, I tried to use this C code, but got errors:

kiss_fft_cpx *cx_in = new kiss_fft_cpx[nfft];  
kiss_fft_cpx *cx_out = new kiss_fft_cpx[nfft]; 

in this two lines. Okay there is no new in C. I tried to modify it but I can not do it. I tried

kiss_fft_cpx *cx_in[1024];
kiss_fft_cpx *cx_out[1024];

and few lines after i tried to pass some value with

cx_in[brojac]->r = i; // this is where program breaks
cx_in[brojac]->i = q;   

from kiss_fft.h header file

typedef struct {
   kiss_fft_scalar r;
   kiss_fft_scalar i;
} kiss_fft_cpx;

typedef struct kiss_fft_state* kiss_f;

//beginning of main 
kiss_fft_cpx *cx_in[1024];
kiss_fft_cpx *cx_out[1024];

//after doing some sampling 
cx_in[brojac]->r = i; // this is where program crash
cx_in[brojac]->i = q;
c
visual-studio
struct
fft
asked on Stack Overflow Aug 29, 2019 by Alexandar13 • edited Aug 29, 2019 by Cris Luengo

2 Answers

1
kiss_fft_cpx *cx_in = new kiss_fft_cpx[nfft];  

In C++ this will allocate an array of structs. The analogous part in C is

struct kiss_fft_cpx *cx_in = malloc(nfft * sizeof(struct kiss_fft_cpx));  

You can use this as

cx_in[brojac].r = i;  // where 0 <= brojac < nfft
answered on Stack Overflow Aug 29, 2019 by Rishikesh Raje
0

cx_in and cx_out are just pointers to an array of structs. You need to allocate the memory.

kiss_fft_cpx *cx_in = malloc(1024*sizeof(kiss_fft_cpx));
kiss_fft_cpx *cx_out = malloc(1024*sizeof(kiss_fft_cpx)); 
answered on Stack Overflow Aug 29, 2019 by Tscheppe

User contributions licensed under CC BY-SA 3.0