简体   繁体   English

C malloc 指向函数内部指针的指针没有给出正确的大小

[英]C malloc of pointer to pointer inside function not giving correct size

So I am now rewriting my fortran code in C (to use CUDA), and apparently I do not understand how to properly use malloc and pointers.所以我现在正在用 C 重写我的 fortran 代码(使用 CUDA),显然我不明白如何正确使用 malloc 和指针。 I am trying to make the main function just calls to other functions, which need to malloc arrays that will then be used inside other functions.我试图让 main 函数只调用其他函数,这些函数需要 malloc 数组,然后将在其他函数中使用。 So, I am passing pointers of pointers to them as per this post: C Programming: malloc() inside another function所以,我按照这篇文章传递了指向它们的指针: C Programming: malloc() inside another function

But the right amount of memory is not being allocated so I get segmentation faults.但是没有分配正确数量的内存,所以我得到了分段错误。 Here is the code:这是代码:

    #include <stdio.h>
    #include <stdlib.h>
    //#include <cuda.h>
    #include <math.h>
    //#include "cublas.h"


    //datatype to match FORTRAN complex type
    typedef float real;

    typedef struct{

        int nx;
        int ny;
        int nz;
        int sz;
        int tz;

    } states;

    void set_SPB(real **,int,states **,states **,int **);
    //void set_SPB();
    int find_minimum(int a[], int n,int start);
    const real hc =197.32697,pi=3.1415927;
    int main(){

        int nmax = 2, A = 28;

         real *etemp, *fock;
         int *Ndex,*lookup,*lookup_a;

         states *channel,*SPB;


        //!generates the single particle basis to be used
        set_SPB(&etemp,nmax,&SPB,&channel,&Ndex);  

        free(etemp);
        free(Ndex);
        free(SPB);

        return 0;
    }
    void set_SPB(real **etemp,int nmax,states **SPB,states **channel,int **Ndex){

         int tot_orbs = (2*nmax+1)*(2*nmax+1)*(2*nmax+1)*4;
         int D = tot_orbs/4;
         int Nalpha =  (2*nmax+1)*(2*nmax+1)*(2*nmax+1)*9;
         real E;

         *etemp = (real*)malloc(D);
         *Ndex = (int*)malloc(D*3);
         *SPB = (states*)malloc(tot_orbs);
          printf("orbits without spin degeneracy %d \n",D);
          printf("size of etemp %ld \n",sizeof(*etemp)/sizeof(*etemp[0]));
          return;
         int i = 0;
        for(int nx =-nmax;nx<=nmax;nx++){
             for(int ny =-nmax;ny<=nmax;ny++){
                 for(int nz =-nmax;nz<=nmax;nz++){
                     E = 0.5*4.0*pi*pi*(nx*nx+ny*ny+nz*nz);
                     //printf("%d\n",i);
                     *etemp[i] = E;
                     *Ndex[0*D+i] =nx;
                     *Ndex[1*D+i] = ny;
                     *Ndex[2*D+i] = nz;
                     i+=1;

                  }
              }
         }

         return;
       }

Also I am not sure exactly if my assignments of the arrays are correct.此外,我不确定我对数组的分配是否正确。 Specifically the print to find the number of elements of that have been allocated always gives 2, when it should be D = 125.具体来说,用于查找已分配元素数的打印总是给出 2,而当它应该是 D = 125 时。

I cannot believe that float and int take only 1 byte in your environment.我不敢相信floatint在您的环境中只占用 1 个字节。 Multiply the size to be allocated by size of their elements.将要分配的大小乘以其元素的大小。

*etemp = malloc(sizeof(**etemp) * D);
*Ndex = malloc(sizeof(**Ndex) * D*3);
*SPB = malloc(sizeof(**SPB) * tot_orbs); /* not sure because this is not used */

Note that they say you shouldn't cast the result of malloc() in C .请注意,他们说你不应该在 C 中转换malloc()的结果

Also note that [] operator has higher precedence than * operator, so you have to use parentheses to use the arrays.另请注意, []运算符的优先级高于*运算符,因此您必须使用括号来使用数组。

(*etemp)[i] = E;
(*Ndex)[0*D+i] =nx;
(*Ndex)[1*D+i] = ny;
(*Ndex)[2*D+i] = nz;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM