简体   繁体   English

我如何初始化双指针有什么问题?

[英]What is wrong with how I initialized a double pointer?

When I run my program just hangs. 当我运行程序时,它挂起了。 Is there anything wrong with the my double pointer setup? 我的双指针设置有什么问题吗? If I comment this out my program doesnt hang. 如果我对此发表评论,我的程序不会挂起。

double **transverse;
transverse = malloc(rows*sizeof(double*));  //allocate memory to double pointer
for (i = 0; i < rows; i++)
{
    transverse[i] = malloc(rows*sizeof(double*));
    for (j = 0; j < rows; j++)
        transverse[i][j] = 0;
}

Your second allocation is wrong 您的第二次分配错误

transverse[i] = malloc(rows*sizeof(double*));

It should be 它应该是

transverse[i] = (double*)malloc(rows*sizeof(double));

traverse[i] is a double * , therefore, you need to use sizeof(double) . traverse[i]double * ,因此,您需要使用sizeof(double)

for (i = 0; i < rows; i++)
{
    //Just double for allocating memory for double pointer transverse[i]
    transverse[i] = malloc(rows*sizeof(double));  //<-- remove double*
    for (j = 0; j < rows; j++)
        transverse[i][j] = 0;
}

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

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