函数内部指针的C malloc未给出正确的大小
所以我现在正在用C重写fortran代码(使用CUDA),显然我不知道如何正确使用malloc和指针。我试图使主函数只调用其他函数,这些函数需要malloc数组,然后这些数组将在其他函数中使用。因此,我根据这篇文章将指针的指针传递给他们: 但是没有分配正确的内存量,因此出现了分段错误。代码如下:函数内部指针的C malloc未给出正确的大小,c,C,所以我现在正在用C重写fortran代码(使用CUDA),显然我不知道如何正确使用malloc和指针。我试图使主函数只调用其他函数,这些函数需要malloc数组,然后这些数组将在其他函数中使用。因此,我根据这篇文章将指针的指针传递给他们: 但是没有分配正确的内存量,因此出现了分段错误。代码如下: #include <stdio.h> #include <stdlib.h> //#include <cuda.h> #include
#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;
}
#包括
#包括
//#包括
#包括
//#包括“cublas.h”
//要匹配FORTRAN复杂类型的数据类型
typedef浮点实数;
类型定义结构{
int-nx;
国际纽约;
国际新西兰;
int sz;
INTTZ;
}国家;
void set_SPB(实**,int,states**,states**,int**);
//void set_SPB();
int find_最小值(int a[],int n,int start);
常数实hc=197.32697,pi=3.1415927;
int main(){
int nmax=2,A=28;
real*etemp,*fock;
int*Ndex,*lookup,*lookup\u a;
状态*通道,*SPB;
//!生成要使用的单粒子基
设置\u SPB(&etemp,nmax,&SPB,&channel,&Ndex);
免费(etemp);
免费(Ndex);
免费(SPB);
返回0;
}
无效集\u SPB(实**etemp,整数nmax,状态**SPB,状态**通道,整数**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;
真实E;
*etemp=(实*)malloc(D);
*Ndex=(int*)malloc(D*3);
*SPB=(州*)malloc(tot_orbs);
printf(“没有自旋简并的轨道%d\n”,d);
printf(“etemp的大小%ld\n”,sizeof(*etemp)/sizeof(*etemp[0]);
返回;
int i=0;
对于(int-nx=-nmax;nx我不能相信float
和int
在您的环境中只占用1个字节。
将要分配的大小乘以其元素的大小
*etemp = malloc(sizeof(**etemp) * D);
*Ndex = malloc(sizeof(**Ndex) * D*3);
*SPB = malloc(sizeof(**SPB) * tot_orbs); /* not sure because this is not used */
注意,他们说
还请注意,[]
运算符的优先级高于*
运算符,因此必须使用括号来使用数组
(*etemp)[i] = E;
(*Ndex)[0*D+i] =nx;
(*Ndex)[1*D+i] = ny;
(*Ndex)[2*D+i] = nz;
如果你想使用CUDA,你应该用C++来重写它,而不是C。什么使C++更有用?对象的方向?我没有任何经验,不幸的是,1。你没有足够的空间。对MALOC的参数是字节数,不是元素的数量。2。代码> sieZof(*ETEMP)/ siZeof(*ETEMP(0))
不是有用的信息。指针上的sizeof
表示指针用于潜在存储其可能指向的块地址的字节数(而不是当前在此类块中分配的字节数)。3.*etemp[i]
应为(*etemp)[i]<代码> >类似NDX。4。检查代码是否> Malc C/Cube >失败。@ PcSvrd:CUDA是基于C++的,而不是C语言和C++是不同的语言。相同的语法可以有不同的语义。从C开始,希望最好的Luton不是一个好主意。这使得它运行,并且我相信正确地存储东西。但是,我确实得到了。“**Error in`./a.out':double-free或corruption(!prev):0x0000000001379010***Aborted”,当我注释掉free()命令后,该错误将消失。sizeof(*etemp)/sizeof(*etemp[0])仍然返回2。抱歉,不必客气。我没有像应该做的那样立即进行所有更改。对于任何混淆,我深表歉意-非常感谢。sizeof(*etemp)/sizeof(*etemp[0])
不是了解分配大小的有效方法。