used uninitialized value of size 8 by strcat using C++ with Valgrind while saving data

断了今生、忘了曾经 提交于 2021-01-29 20:52:00

问题


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);


回答1:


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.




回答2:


You could save a lot of bother by simply using C++ strings.

So you could write

print2DArrf(Nfilename, std::string("N") + std::to_string(saveNumber) + ".txt");

(this would require you to change print2DArrf to take an std::string (or const reference to one).

You don't say which version of C++ you are using, so you may need to use something other than to_string if it is old C++.

Last comment, Valgrind on macOS isn't very well supported, so use it with some caution.



来源:https://stackoverflow.com/questions/64346021/used-uninitialized-value-of-size-8-by-strcat-using-c-with-valgrind-while-savin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!