使用valgrind调试c,写入/读取大小为4

时间:2017-07-15 07:15:16

标签: c valgrind

我已经阅读了一些有关valgrind的帖子,但我仍然无法自己解决。

所以我有一个C脚本,最初工作得很好。我稍后用一些新的输入修改了它,然后我在我的一个主要for循环中得到了一个分段错误。

我实际上找到了一些printf函数的错误,我做了一些实验来确定导致错误的确切行。但是我找到的位置中没有一条线对分段故障很重要。我还仔细检查了我的malloc函数和我的指针,即使它们在原始版本中工作。

然后我读了一些关于valgrind的帖子,这就是我得到的:

==21733== Invalid write of size 4
==21733==    at 0x4014A4: h_spin_vor_amp (in /mnt/scratch-            
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  Address 0x11c27208 is 0 bytes after a block of size 
209,584,584 alloc'd
==21733==    at 0x4C27A2E: malloc (vg_replace_malloc.c:270)
==21733==    by 0x402954: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733== 
==21733== Invalid write of size 4
==21733==    at 0x4014E9: h_spin_vor_amp (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  Address 0x1e408208 is 0 bytes after a block of size 
209,584,584 alloc'd
==21733==    at 0x4C27A2E: malloc (vg_replace_malloc.c:270)
==21733==    by 0x40297F: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733== 
==21733== Invalid write of size 4
==21733==    at 0x40152E: h_spin_vor_amp (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  Address 0x2abe9208 is 0 bytes after a block of size 
209,584,584 alloc'd
==21733==    at 0x4C27A2E: malloc (vg_replace_malloc.c:270)
==21733==    by 0x4029AA: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733== 
==21733== Invalid write of size 4
==21733==    at 0x401573: h_spin_vor_amp (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  Address 0x373ca208 is 0 bytes after a block of size 
209,584,584 alloc'd
==21733==    at 0x4C27A2E: malloc (vg_replace_malloc.c:270)
==21733==    by 0x4029D5: h_spin_shellwriter (in /mnt/scratch-lus 
tre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733== 
test0 test1 test2 test3 test4 
4475
test0 test1 test2 test3 test4 
4476
==21733== Invalid read of size 4
==21733==    at 0x400A30: h_spin_vor_amp (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  Address 0xffffffff34cc3cdc is not stack'd, malloc'd or (r 
ecently) free'd
==21733== 
==21733== 
==21733== Process terminating with default action of signal 11 
(SIGSEGV)
==21733==  Access not within mapped region at address 
0xFFFFFFFF34CC3CDC
==21733==    at 0x400A30: h_spin_vor_amp (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  If you believe this happened as a result of a stack
==21733==  overflow in your program's main thread (unlikely but
==21733==  possible), you can try to increase the size of the
==21733==  main thread stack using the --main-stacksize= flag.
==21733==  The main thread stack size used in this run was 10485760.
==21733== Invalid read of size 4
==21733==    at 0x5126D7F: _IO_flush_all_lockp (in /lib64/libc-2.12.so)
==21733==    by 0x5127C5E: _IO_cleanup (in /lib64/libc-2.12.so)
==21733==    by 0x51F3E62: __libc_freeres (in /lib64/libc-2.12.so)
==21733==    by 0x4A2266D: _vgnU_freeres (vg_preloaded.c:62)
==21733==    by 0x7FEFFF0C7: ???
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  Address 0xc2c80000c2c800c0 is not stack'd, malloc'd or 
(recently) free'd
==21733== 
==21733== 
==21733== Process terminating with default action of signal 11 (SIGSEGV)
==21733==  General Protection Fault
==21733==    at 0x5126D7F: _IO_flush_all_lockp (in /lib64/libc-2.12.so)
==21733==    by 0x5127C5E: _IO_cleanup (in /lib64/libc-2.12.so)
==21733==    by 0x51F3E62: __libc_freeres (in /lib64/libc-2.12.so)
==21733==    by 0x4A2266D: _vgnU_freeres (vg_preloaded.c:62)
==21733==    by 0x7FEFFF0C7: ???
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733== 
==21733== HEAP SUMMARY:
==21733==     in use at exit: 16,767,087,832 bytes in 30 blocks
==21733==   total heap usage: 30 allocs, 0 frees, 16,767,087,832 bytes 
allocated
==21733== 
==21733== LEAK SUMMARY:
==21733==    definitely lost: 0 bytes in 0 blocks
==21733==    indirectly lost: 0 bytes in 0 blocks
==21733==      possibly lost: 0 bytes in 0 blocks
==21733==    still reachable: 16,767,087,832 bytes in 30 blocks
==21733==         suppressed: 0 bytes in 0 blocks
==21733== Rerun with --leak-check=full to see details of leaked memory
==21733== 
==21733== For counts of detected and suppressed errors, rerun with: -v
==21733== ERROR SUMMARY: 218519 errors from 6 contexts (suppressed: 6 
from 6)

谢谢!

======编辑===========

感谢您的建议,现在我在上面上传我的代码。这段代码的作用是读取我的python代码处理的一些模拟数据。我所拥有的是一个尺寸为(512,512,512)的模拟盒。里面有一些叫做光晕和形态(如SFS)的东西。 Halos有旋转,形态有涡度,我想知道晕圈周围的壳,旋转和涡度之间的余弦是什么。另外,我想知道余弦旋转的余弦和振幅之间的关系。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define ArrayAccess2D_n2(a, n1, n2, i1, i2) (a)[ i2+n2*i1 ]
#define ArrayAccess3D_n3(a, n1, n2, n3, i, j, k) ((a)[ ((n2)*(i)+(j))*(n3)+(k) ])
#define ArrayAccess4D_n4(a, n1, n2, n3, n4, i1, i2, i3, i4) ((a)[ i4+n4*(i3+n3*(i2+n2*i1)) ])
#define ArrayAccess5D_n5(a, n1, n2, n3, n4, n5, i1, i2, i3, i4, i5) ((a)[ i5+n5*(i4+n4*(i3+n3*(i2+n2*i1))) ])
#define dotproduct(x1, y1, z1, x2, y2, z2) (x1*x2 + y1*y2 + z1*z2)


//cos between vec (x1, y1, z1) and (x2, y2, z2)
//if there is zero vector, set impossible value -100
float veccos(float x1, float y1, float z1, float x2, float y2, float z2)
{
    float cos, dot1, dot2;
    dot1 = dotproduct(x1, y1, z1, x1, y1, z1);
    dot2 = dotproduct(x2, y2, z2, x2, y2, z2);

    if(dot1!=0 && dot2!=0){
        cos = dotproduct(x1, y1, z1, x2, y2, z2)/(sqrt(dot1)*sqrt(dot2));
    }
    else{
        cos = -100;
    }
    return cos;
}

//omega in py is (4,3,ng,ng,ng) array with padding, in order sfs, sfc, ufs, ufc
//halo_spin shape (3,ng,ng,ng) with padding
//halo_g of (3, halo_num), halo_R of (halo_num,) in unit of grids
void h_spin_vor_amp(float *halo_spinlist, int *halo_spinamp, int *halo_g, float *halo_R, float *omega, int halo_start, int halo_end, int halo_num, int ng, float Lbox, int lowbound_fac, int bound_fac, int padding, int ngp, float *out_sfs, float *out_sfc, float *out_ufs, float *out_ufc)
{
    long long iter=0;
    int g_x, g_y, g_z, g_R; //coordinator on grids
    int halo_x1, halo_x2, halo_y1, halo_y2, halo_z1, halo_z2;
    int ext_x1, ext_x2, ext_y1, ext_y2, ext_z1, ext_z2;
    float phy_R, h_sx, h_sy, h_sz;
    int i, j;

    //temporary array, recording cos of each type
        float *cos_sfst, *cos_sfct, *cos_ufst, *cos_ufct;
        cos_sfst = (float *) malloc(ngp*ngp*ngp*sizeof(float));
        cos_sfct = (float *) malloc(ngp*ngp*ngp*sizeof(float));
        cos_ufst = (float *) malloc(ngp*ngp*ngp*sizeof(float));
        cos_ufct = (float *) malloc(ngp*ngp*ngp*sizeof(float));

    for(long j=halo_start; j<halo_end; j++){
        printf("%d\n", j);

        int i;
        i=halo_spinamp[j];

                printf("test0 ");
        g_x = ArrayAccess2D_n2(halo_g, 3, halo_num, 0, i);
                g_y = ArrayAccess2D_n2(halo_g, 3, halo_num, 1, i);
                g_z = ArrayAccess2D_n2(halo_g, 3, halo_num, 2, i);

        phy_R = halo_R[i];
        g_R = phy_R*ng/(1000*Lbox);

        if( (phy_R*ng/(1000*Lbox) - g_R) > 0.5){
            g_R += 1;
        }

                halo_x1 = g_x - lowbound_fac*g_R; //boundary of halo edges
                halo_x2 = g_x + lowbound_fac*g_R;

                halo_y1 = g_y - lowbound_fac*g_R; //e.g. y1 < y2
                halo_y2 = g_y + lowbound_fac*g_R;

                halo_z1 = g_z - lowbound_fac*g_R;
                halo_z2 = g_z + lowbound_fac*g_R;

                ext_x1 = g_x - bound_fac*g_R; //boundary of extended edges
                ext_x2 = g_x + bound_fac*g_R;

                ext_y1 = g_y - bound_fac*g_R;
                ext_y2 = g_y + bound_fac*g_R;

                ext_z1 = g_z - bound_fac*g_R;
                ext_z2 = g_z + bound_fac*g_R;
                printf("test1 ");
        //compute cos of all in boundary
        for(long x=ext_x1+padding-1; x<ext_x2+padding; x++){
         for(long y=ext_y1+padding-1; y<ext_y2+padding; y++){
          for(long z=ext_z1+padding-1; z<ext_z2+padding; z++){

                        h_sx = ArrayAccess2D_n2(halo_spinlist, 3, halo_num, 0, i);
                        h_sy = ArrayAccess2D_n2(halo_spinlist, 3, halo_num, 1, i);
                        h_sz = ArrayAccess2D_n2(halo_spinlist, 3, halo_num, 2, i);

            cos_sfst[(ngp*x + y)*ngp + z] = veccos(ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 0, 0, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 0, 1, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 0, 2, x,y,z), h_sx, h_sy, h_sz);

                    cos_sfct[(ngp*x + y)*ngp + z] = veccos(ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 1, 0, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 1, 1, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 1, 2, x,y,z), h_sx, h_sy, h_sz); 

                    cos_ufst[(ngp*x + y)*ngp + z] = veccos(ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 2, 0, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 2, 1, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 2, 2, x,y,z), h_sx, h_sy, h_sz);

                    cos_ufct[(ngp*x + y)*ngp + z] = veccos(ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 3, 0, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 3, 1, x,y,z), ArrayAccess5D_n5(omega, 4, 3, ngp, ngp, ngp, 3, 2, x,y,z), h_sx, h_sy, h_sz);
          }
         }
        }
                printf("test2 ");
        //set all cos in halo to invalid identifier -100
                for(int x=halo_x1+padding-1; x<halo_x2+padding; x++){
                 for(int y=halo_y1+padding-1; y<halo_y2+padding; y++){
                  for(int z=halo_z1+padding-1; z<halo_z2+padding; z++){
                    cos_sfst[(ngp*x + y)*ngp + z] = -100;
                    cos_sfct[(ngp*x + y)*ngp + z] = -100;
                    cos_ufst[(ngp*x + y)*ngp + z] = -100;
                    cos_ufct[(ngp*x + y)*ngp + z] = -100;
                  }
                 }
                }
                printf("test3 ");
        //recording cos
                for(int x=ext_x1+padding-1; x<ext_x2+padding; x++){
                 for(int y=ext_y1+padding-1; y<ext_y2+padding; y++){
                  for(int z=ext_z1+padding-1; z<ext_z2+padding; z++){

            out_sfs[iter] = cos_sfst[(ngp*x + y)*ngp + z];
            out_sfc[iter] = cos_sfct[(ngp*x + y)*ngp + z];
                    out_ufs[iter] = cos_ufst[(ngp*x + y)*ngp + z];
                    out_ufc[iter] = cos_ufct[(ngp*x + y)*ngp + z];

                        cos_sfst[(ngp*x + y)*ngp + z] = 0;
                        cos_sfct[(ngp*x + y)*ngp + z] = 0;
                        cos_ufst[(ngp*x + y)*ngp + z] = 0;
                        cos_ufct[(ngp*x + y)*ngp + z] = 0;

            iter++;
          }
         }
        }
                printf("test4 \n");
    }
    printf("iterate upto %lli \n", iter);
}

void h_spin_shellwriter(float Lbox, int ng, long ngp, char *sim_num, char *sm_name, int halo_num, int *bins, int lowbound_fac, int upbound_fac, int padding){
    //get shell
    char shell[10], shell1[10];
    sprintf(shell1, "_r%dto%d", lowbound_fac, upbound_fac); 
    sprintf(shell, "_r%dto%d", lowbound_fac, upbound_fac);  
    //sprintf(shell, "_c%dto%d_b1n2", lowbound_fac, upbound_fac);
    printf("for shell%s\n", shell);


    printf("initializing memory space\n");
    //file name, routes
    char in_halo_s[100], in_halo_g[100], in_halo_R[100], in_omega[100], in_halo_amp[100];

    sprintf(in_halo_s, "../gadgetsim/halo_spinout/100Mpc/pyout%s/gadg_u%s_halo_sls%s.dat", shell1, sim_num, sm_name); //fixedrad
    //sprintf(in_halo_s, "../gadgetsim/halo_spinout/100Mpc/pyout%s/gadg_u%s_halo_s%s.dat", shell, sim_num, sm_name);
    sprintf(in_halo_g, "../gadgetsim/halo_spinout/100Mpc/pyout%s/gadg_u%s_halo_g%s.dat", shell1, sim_num, sm_name);
    sprintf(in_halo_R, "../gadgetsim/halo_spinout/100Mpc/pyout%s/gadg_u%s_halo_R%s.dat", shell1, sim_num, sm_name);
    sprintf(in_omega, "../gadgetsim/halo_spinout/100Mpc/pyout%s/gadg_u%s_halo_omega%s.dat", shell1, sim_num, sm_name);
    sprintf(in_halo_amp, "../gadgetsim/halo_spinout/100Mpc/pyout%s/gadg_u%s_halo_ampsort%s.dat", shell1, sim_num, sm_name);

    char out_sfsb1[100], out_sfcb1[100], out_ufsb1[100], out_ufcb1[100];

    sprintf(out_sfsb1, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfsb1_%s.dat", shell, sim_num, sm_name);
    sprintf(out_sfcb1, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfcb1_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufsb1, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufsb1_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufcb1, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufcb1_%s.dat", shell, sim_num, sm_name);

    char out_sfsb2[100], out_sfcb2[100], out_ufsb2[100], out_ufcb2[100];

    sprintf(out_sfsb2, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfsb2_%s.dat", shell, sim_num, sm_name);
    sprintf(out_sfcb2, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfcb2_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufsb2, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufsb2_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufcb2, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufcb2_%s.dat", shell, sim_num, sm_name);

    char out_sfsb3[100], out_sfcb3[100], out_ufsb3[100], out_ufcb3[100];

    sprintf(out_sfsb3, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfsb3_%s.dat", shell, sim_num, sm_name);
    sprintf(out_sfcb3, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfcb3_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufsb3, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufsb3_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufcb3, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufcb3_%s.dat", shell, sim_num, sm_name);

    char out_sfsb4[100], out_sfcb4[100], out_ufsb4[100], out_ufcb4[100];

    sprintf(out_sfsb4, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfsb4_%s.dat", shell, sim_num, sm_name);
    sprintf(out_sfcb4, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cossfcb4_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufsb4, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufsb4_%s.dat", shell, sim_num, sm_name);
    sprintf(out_ufcb4, "../gadgetsim/halo_spinout/100Mpc/ampbin%s/gadg_u%s_halo_cosufcb4_%s.dat", shell, sim_num, sm_name);


    //array for recording
    float *cos_sfsb1, *cos_sfcb1, *cos_ufsb1, *cos_ufcb1;
    float *cos_sfsb2, *cos_sfcb2, *cos_ufsb2, *cos_ufcb2;
    float *cos_sfsb3, *cos_sfcb3, *cos_ufsb3, *cos_ufcb3;
    float *cos_sfsb4, *cos_sfcb4, *cos_ufsb4, *cos_ufcb4;

    cos_sfsb1 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_sfcb1 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufsb1 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufcb1 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);

    cos_sfsb2 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_sfcb2 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufsb2 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufcb2 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);

    cos_sfsb3 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_sfcb3 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufsb3 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufcb3 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);

    cos_sfsb4 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_sfcb4 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufsb4 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);
    cos_ufcb4 = (float *) malloc(ngp*ngp*ngp*sizeof(float)/4);


    //initialize outputs
    for(int i=0; i<ngp*ngp*ngp/4; i++){
            cos_sfsb1[i] = -100;
            cos_sfcb1[i] = -100;
            cos_ufsb1[i] = -100;
            cos_ufcb1[i] = -100;

            cos_sfsb2[i] = -100;
            cos_sfcb2[i] = -100;
            cos_ufsb2[i] = -100;
            cos_ufcb2[i] = -100;

            cos_sfsb3[i] = -100;
            cos_sfcb3[i] = -100;
            cos_ufsb3[i] = -100;
            cos_ufcb3[i] = -100;

            cos_sfsb4[i] = -100;
            cos_sfcb4[i] = -100;
            cos_ufsb4[i] = -100;
            cos_ufcb4[i] = -100;
    }

    printf("reading halo data\n");
    float *halo_s, *halo_R;
    int *halo_g, *halo_amp;

    halo_s = (float *) malloc(3*halo_num*sizeof(float));
    //halo_s = (float *) malloc(3*ngp*ngp*ngp * sizeof(float));
    halo_g = (int *) malloc(3*halo_num*sizeof(int));
    halo_R = (float *) malloc(halo_num * sizeof(float));
    halo_amp = (int *) malloc(halo_num*sizeof(int));

    float *omega;
    omega = (float *) malloc(4*3*ngp*ngp*ngp * sizeof(float));

    FILE *F_halo_s, *F_halo_g, *F_halo_R, *F_halo_amp;
    FILE *F_omega;

    F_halo_s = fopen(in_halo_s, "rb");
    F_halo_g = fopen(in_halo_g, "rb");
    F_halo_R = fopen(in_halo_R, "rb");
    F_halo_amp = fopen(in_halo_amp, "rb");

    F_omega = fopen(in_omega, "rb");

    fread(halo_s, sizeof(float), 3*halo_num, F_halo_s);
    //fread(halo_s, sizeof(float), 3*ngp*ngp*ngp, F_halo_s);
    fread(halo_g, sizeof(int), 3*halo_num, F_halo_g);
    fread(halo_R, sizeof(float), halo_num, F_halo_R);
    fread(halo_amp, sizeof(int), halo_num, F_halo_amp);
    fread(omega, sizeof(float), 4*3*ngp*ngp*ngp, F_omega);

    printf("computing cosine...\n");

        //get physical radius mean
        float phy_R_mean=0;
    int g_R_mean=0;

    /*
        for(int i=0; i<bins[1]; i++){
        phy_R_mean = phy_R_mean + halo_R[i]; }
        phy_R_mean = phy_R_mean/bins[1];
        printf("%d, %f\n", bins[1], phy_R_mean); 
        //get mean radius in grid
        g_R_mean = phy_R_mean*ng/(1000*Lbox);
        if((phy_R_mean*ng/(1000*Lbox) - g_R_mean) > 0.5){
        g_R_mean += 1; }
    printf("mean of radius in grids: %f\n", g_R_mean);
    */

    /*
        h_spin_vor_fixedrad(halo_s, halo_g, g_R_mean, omega, 0, bins[0], halo_num, ng, Lbox, lowbound_fac, upbound_fac, padding, ngp, cos_sfsb1, cos_sfcb1, cos_ufsb1, cos_ufcb1);

        h_spin_vor_fixedrad(halo_s, halo_g, g_R_mean, omega, bins[0], bins[1], halo_num, ng, Lbox, lowbound_fac, upbound_fac, padding, ngp, cos_sfsb2, cos_sfcb2, cos_ufsb2, cos_ufcb2);
    */

    h_spin_vor_amp(halo_s, halo_amp, halo_g, halo_R, omega, 0, bins[0], halo_num, ng, Lbox, lowbound_fac, upbound_fac, padding, ngp, cos_sfsb1, cos_sfcb1, cos_ufsb1, cos_ufcb1);

    h_spin_vor_amp(halo_s, halo_amp, halo_g, halo_R, omega, bins[0], bins[1], halo_num, ng, Lbox, lowbound_fac, upbound_fac, padding, ngp, cos_sfsb2, cos_sfcb2, cos_ufsb2, cos_ufcb2);

    h_spin_vor_amp(halo_s, halo_amp, halo_g, halo_R, omega, bins[1], bins[2], halo_num, ng, Lbox, lowbound_fac, upbound_fac, padding, ngp, cos_sfsb3, cos_sfcb3, cos_ufsb3, cos_ufcb3);

    h_spin_vor_amp(halo_s, halo_amp, halo_g, halo_R, omega, bins[2], bins[3], halo_num, ng, Lbox, lowbound_fac, upbound_fac, padding, ngp, cos_sfsb4, cos_sfcb4, cos_ufsb4, cos_ufcb4);


    printf("computation done\n");
    free(halo_s);
    free(halo_g);
    free(halo_R);
    free(halo_amp);
    free(omega);

    printf("writing out the final data...\n");
    FILE *F_out_sfsb1, *F_out_sfcb1, *F_out_ufsb1, *F_out_ufcb1;
    FILE *F_out_sfsb2, *F_out_sfcb2, *F_out_ufsb2, *F_out_ufcb2;
    FILE *F_out_sfsb3, *F_out_sfcb3, *F_out_ufsb3, *F_out_ufcb3;
    FILE *F_out_sfsb4, *F_out_sfcb4, *F_out_ufsb4, *F_out_ufcb4;


    F_out_sfsb1 = fopen(out_sfsb1, "wb");
    F_out_sfcb1 = fopen(out_sfcb1, "wb");
    F_out_ufsb1 = fopen(out_ufsb1, "wb");
    F_out_ufcb1 = fopen(out_ufcb1, "wb");

    fwrite(cos_sfsb1, sizeof(float), ngp*ngp*ngp/4, F_out_sfsb1);
    fwrite(cos_sfcb1, sizeof(float), ngp*ngp*ngp/4, F_out_sfcb1);
    fwrite(cos_ufsb1, sizeof(float), ngp*ngp*ngp/4, F_out_ufsb1);
    fwrite(cos_ufcb1, sizeof(float), ngp*ngp*ngp/4, F_out_ufcb1);

    //free(cos_sfsb1), free(cos_sfcb1), free(cos_ufsb1), free(cos_ufcb1);


    F_out_sfsb2 = fopen(out_sfsb2, "wb");
    F_out_sfcb2 = fopen(out_sfcb2, "wb");
    F_out_ufsb2 = fopen(out_ufsb2, "wb");
    F_out_ufcb2 = fopen(out_ufcb2, "wb");

    fwrite(cos_sfsb2, sizeof(float), ngp*ngp*ngp/4, F_out_sfsb2);
    fwrite(cos_sfcb2, sizeof(float), ngp*ngp*ngp/4, F_out_sfcb2);
    fwrite(cos_ufsb2, sizeof(float), ngp*ngp*ngp/4, F_out_ufsb2);
    fwrite(cos_ufcb2, sizeof(float), ngp*ngp*ngp/4, F_out_ufcb2);

       //free(cos_sfsb2), free(cos_sfcb2), free(cos_ufsb2), free(cos_ufcb2);

/*  
    F_out_sfsb3 = fopen(out_sfsb3, "wb");
    F_out_sfcb3 = fopen(out_sfcb3, "wb");
    F_out_ufsb3 = fopen(out_ufsb3, "wb");
    F_out_ufcb3 = fopen(out_ufcb3, "wb");

    fwrite(cos_sfsb3, sizeof(float), ngp*ngp*ngp/4, F_out_sfsb3);
    fwrite(cos_sfcb3, sizeof(float), ngp*ngp*ngp/4, F_out_sfcb3);
    fwrite(cos_ufsb3, sizeof(float), ngp*ngp*ngp/4, F_out_ufsb3);
    fwrite(cos_ufcb3, sizeof(float), ngp*ngp*ngp/4, F_out_ufcb3);

        //free(cos_sfsb3), free(cos_sfcb3), free(cos_ufsb3), free(cos_ufcb3);


    F_out_sfsb4 = fopen(out_sfsb4, "wb");
    F_out_sfcb4 = fopen(out_sfcb4, "wb");
    F_out_ufsb4 = fopen(out_ufsb4, "wb");
    F_out_ufcb4 = fopen(out_ufcb4, "wb");

    fwrite(cos_sfsb4, sizeof(float), ngp*ngp*ngp/4, F_out_sfsb4);
    fwrite(cos_sfcb4, sizeof(float), ngp*ngp*ngp/4, F_out_sfcb4);
    fwrite(cos_ufsb4, sizeof(float), ngp*ngp*ngp/4, F_out_ufsb4);
    fwrite(cos_ufcb4, sizeof(float), ngp*ngp*ngp/4, F_out_ufcb4);

        //free(cos_sfsb4), free(cos_sfcb4), free(cos_ufsb4), free(cos_ufcb4);
*/
}

int main()
{

//basic info of the set of simulations
float Lbox=100;
int ng=512;
int u0_halo_num=9946, u1_halo_num=9897, u2_halo_num=10380;
int u3_halo_num=9801, u4_halo_num=10062, u5_halo_num=10170;
int u6_halo_num=10028, u7_halo_num=10244, u8_halo_num=9925;
int u9_halo_num=10329, u10_halo_num=10168;

int bins[4];
int u0_bins[] = {8766, 9860, 9942, 9946}; //[  0.   0.1  0.2   0.3  0.4 ]
int u1_bins[] = {8770, 9812, 9892, 9897};
int u2_bins[] = {9270, 10282, 10371, 10380};
int u3_bins[] = {8645, 9720, 9799, 9801};
int u4_bins[] = {8862, 9966, 10061, 10062};
int u5_bins[] = {9023, 10074, 10164, 10170};
int u6_bins[] = {8884, 9924, 10021, 10027};
int u7_bins[] = {9119, 10156, 10236, 10244};
int u8_bins[] = {8849, 9848, 9923, 9925};
int u9_bins[] = {9213, 10240, 10321, 10329};
int u10_bins[] = {8984, 10073, 10159, 10168};

int lowbound_fac, bound_fac, padding0, padding1, padding2;
int padding3, padding4, padding5, padding6, padding7, padding8;
int padding9, padding10;
long ngp;
/*====================================*/


//control section
/*===============*/

//smoothing scale choice
char sm_name[] = "02";

//choose simulations
char sim_num[] = "0";
int sim_intnum = 0;
int halo_num = u0_halo_num;

for(int i=0; i<4; i++){
     bins[i] = u0_bins[i]; //change simulation number here too
}
printf("computing for u%s\n\n", sim_num);
/*============================*/


//compute for different shells

lowbound_fac=3, bound_fac=5, padding0=41, padding1=45, padding2=47;
padding3=54, padding4=49, padding5=35, padding6=37, padding7=47, padding8=48, padding9=47, padding10=51;

int padding_r3to5[] = {padding0, padding1, padding2, padding3, padding4, padding5,
            padding6, padding7, padding8, padding9, padding10};
ngp=ng+2*padding_r3to5[sim_intnum];

h_spin_shellwriter(Lbox, ng, ngp, sim_num, sm_name, halo_num, bins, lowbound_fac, bound_fac, padding_r3to5[sim_intnum]);


lowbound_fac=2, bound_fac=3, padding0=25, padding1=27, padding2=29;
padding3=33, padding4=30, padding5=21, padding6=23, padding7=29, padding8=29, padding9=29, padding10=31;

int padding_r2to3[] = {padding0, padding1, padding2, padding3, padding4, padding5,
                        padding6, padding7, padding8, padding9, padding10};
ngp=ng+2*padding_r2to3[sim_intnum];

h_spin_shellwriter(Lbox, ng, ngp, sim_num, sm_name, halo_num, bins, lowbound_fac, bound_fac, padding_r2to3[sim_intnum]);


lowbound_fac=1, bound_fac=2, padding0=17, padding1=18, padding2=19;
padding3=22, padding4=20, padding5=15, padding6=15, padding7=20, padding8=20, padding9=19, padding10=21;

int padding_r1to2[] = {padding0, padding1, padding2, padding3, padding4, padding5,
                        padding6, padding7, padding8, padding9, padding10};
ngp=ng+2*padding_r1to2[sim_intnum];

h_spin_shellwriter(Lbox, ng, ngp, sim_num, sm_name, halo_num, bins, lowbound_fac, bound_fac, padding_r1to2[sim_intnum]);

}

2 个答案:

答案 0 :(得分:2)

如果没有发布任何源代码,很难帮助你,但如果你在理解valgrind输出时遇到问题,可以用这种方式解释:

你有很多错误,每个都有这种形式:

==21733== Invalid write of size 4
==21733==    at 0x4014A4: h_spin_vor_amp (in /mnt/scratch-            
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==  Address 0x11c27208 is 0 bytes after a block of size 
209,584,584 alloc'd
==21733==    at 0x4C27A2E: malloc (vg_replace_malloc.c:270)
==21733==    by 0x402954: h_spin_shellwriter (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)
==21733==    by 0x403A2C: main (in /mnt/scratch-
lustre/joexu/workplace/myC/halo_samp)

这意味着以下内容:

  1. 您使用来自h_spin_shellwriter的malloc分配了一些内存(~200MB)。
  2. h_spin_vor_amp中执行代码时:
    • 你写了4个字节(uint32,32位系统上的int,或者类似的东西)
    • 在分配的内存(0 bytes after a block...
    • 之后
  3. 最有可能发生的事情是,你正在迭代分配的内存中的所有值,并且走得太远了。或者你可能想在某些数组的末尾添加一些内容而不检查是否还有剩余空间。没有任何代码,这很可能是猜测。

    然而,这可能是一些其他问题 - 意外覆盖某些指针,永久增加指针而不是使用临时,或者只是做错误的偏移计算也可能是原因。

    不同的错误:

    ==21733== Invalid read of size 4
    ==21733==    at 0x400A30: h_spin_vor_amp (in /mnt/scratch-
    lustre/joexu/workplace/myC/halo_samp)
    ==21733==    by 0x403104: h_spin_shellwriter (in /mnt/scratch-
    lustre/joexu/workplace/myC/halo_samp)
    ==21733==    by 0x403A2C: main (in /mnt/scratch-
    lustre/joexu/workplace/myC/halo_samp)
    ==21733==  Address 0xffffffff34cc3cdc is not stack'd, malloc'd or (r 
    ecently) free'd
    

    表示您意外地将指针保存到稍后释放的某个内存,或者您在地址计算中出错。

    奇怪的是,你没有在堆栈跟踪中获得精确的行信息。如果在编译时未启用调试信息,则应启用它们。否则,删除优化可能会让你得到更好的答案。

答案 1 :(得分:0)

使用sprintf的位置,请确保数组参数为空终止。