I am aware of existence of similar posts about segmentation fault, however, I have a bit of a specific issue that I need some guidance with. I am working on a code, it's a simulation that calculates some values and saves all of these values in a document that could be used later for data analysis.
I am getting a segmentation fault error when I run the code with Valgrind in debugging mode I get the following:
"used uninitialized value of size 8":
Iteration = 100 t=10.1000000
==26716== conditional jump or move depends on uninitialised value(s)
==26716== at 0x1007F7910: _platform_memmove$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==26716== by 0x1005BC91: strcat (in /usr/lib/system/libsystem_c.dylib)
==26716== by 0x1000031F2: main (ExampleScript.cpp:594)
Basically, it gives this error for the lines: 594,599,604 which are in the for loop:
593 char KEfilename[5];
594 strcat(KEfilename, "KE");
595 strcat(KEfilename, save);
and line 604:
603 char Pfilename[5];
604 strcat(Pfilename, "P");
605 strcat(Pfilename, save);
The for loop to save my data:
printf("Iteration = %d t = %.10f P_iter = %d\n", iter, time[iter+1], P_iter);
// Save data every frequency time steps
if ((iter/frequency) - saveNumber == 0){
c2rfft(Nk, N);
c2rfft(KEk, KE); //convert complex values of KEk calculated in main to real
c2rfft(KIk, KI);
c2rfft(Pk, P);
char save[3];
snprintf(save, 3, "%d", saveNumber);
const char *type = " .txt";
char Nfilename[5];
strcat(Nfilename, "N");
strcat(Nfilename, save);
char KEfilename[5];
strcat(KEfilename, "KE");
strcat(KEfilename, save);
char KIfilename[5];
strcat(KIfilename, "KI");
strcat(KIfilename, save);
char Pfilename[5];
strcat(Pfilename, "P");
strcat(Pfilename, save);
//print2DArrf: void print2DArrf(char *filename, double *arr)
print2DArrf(Nfilename, N);
print2DArrf(KEfilename, KE);
print2DArrf(KIfilename, KI);
print2DArrf(Pfilename, P);
memset(Nfilename, 0, sizeof Nfilename);
memset(KEfilename, 0, sizeof KEfilename);
memset(KIfilename, 0, sizeof KIfilename);
memset(Pfilename, 0, sizeof Pfilename);
saveNumber++;
It's a loop where I am saving my data to be written in a specific file format "txt." I don't think the issue has to do with "initializing" since I am sort of confident in the way I initialized everything, including pointers. I also, made sure to "free" everything as well.
The segmentation fault happens at the same iteration and t so that's what made me question my method of data saving. Also, I do not have any experience in c++ so would be nice to point my issues and explain why.
A sample of main code:
#include<math.h>
#include<stdio.h>
#include "Functions.h"
#include "fftw3.h"
#include <cstring>
int main(){
// Setting parameters
double frequency = 200.0;
double dt = 0.1;
double tend = 5000.;
double err_max = 1e-7;
int P_iter_max = 500;
int iter_max = tend / dt;
int saveNumber = 1;
//Initialize Parameters
double *N;
N = (double*) fftw_malloc(nx*ny*sizeof(double));
fftw_complex *Nk;
Nk = (fftw_complex*) fftw_malloc(nx*nyk*sizeof(fftw_complex));
double *KIk;
KIk = (double*) fftw_malloc(nx*ny*sizeof(double));
double *KE;
KE = (double*) fftw_malloc(nx*ny*sizeof(double));
fftw_complex *KEk;
KEk = (fftw_complex*) fftw_malloc(nx*nyk* sizeof(fftw_complex));
double *P;
P = (double*) fftw_malloc(nx*ny*sizeof(double));
fftw_complex *Pk;
Pk = (fftw_complex*) fftw_malloc(nx*nyk* sizeof(fftw_complex));
//Save I.Cs
int P_iter = Potk(invnk, dndxk, dndyk, Pk, PSk, kx, ky, ninvksq, err_max, P_iter_max);
for (int iter = 0; iter < iter_max; iter++){
// calculate some values
//Check for convergence
// Save I.C loop
P_iter = Pk(NNk, dndxk, dndyk, phik, potSourcek, kx, ky, ninvksqu, err_max, P_iter_max);
double time[iter_max + 1];
time[0] = 0;
c2rfft(Nk, N);
c2rfft(KEk, KE);
c2rfft(KIk, KI);
c2rfft(Pk, P);
char Ninitial[] = "N_initial.txt";
char KEinitial[] = "KE_initial.txt";
char KIinitial[] = "KI_initial.txt";
char Pinitial[] = "P_initial.txt";
print2DArrf(Ninitial, N);
print2DArrf(KEinitial, KE);
print2DArrf(KIinitial, KI);
print2DArrf(Pinitial, P);
I don't think the issue has to do with "initializing"
You should think otherwise.
char Nfilename[5];
You've default initialised this array. In this case, that means the contents are indeterminate.
strcat(Nfilename, "N");
Here, you pass (pointer to) that array whose contents are indeterminate into strcat
. strcat
requires that the left hand argument is an array which contains a null terminated string (and that the array is sufficiently large for the result string). The array that you passed does not contain a null terminated string, and as a result the behaviour of the program is undefined.
You've repeated this bug several times.