简体   繁体   English

C-访问头文件中的指针时出现分段错误

[英]C - segmentation fault when accessing a pointer in header file

I'm programming in C, and have two files, func2.h : 我正在用C编程,并且有两个文件func2.h

#define NN 20

void network_construction(int **veins, int *num_veins){

        int i, j;


        for(i=0;i<NN;i++){
            num_veins[i] = NN/2;
        }


        veins = malloc(NN * sizeof(*veins));
        for (i = 0; i < NN; i++) { veins[i] = malloc(num_veins[i] * sizeof(*(veins[i]))); }
        for (i = 0; i < NN; i++) { for (j = 0; j<num_veins[i];j++) { veins[i][j] = -1; } }


    return;
    }

and main.c : main.c

#include <stdio.h>
#include <stdlib.h>
#include "func2.h"

void main(){

    int num_veins[NN];
    int **veins;


    network_construction(veins, num_veins);

    printf("\n%d\n", num_veins[19]);
    printf("\n%d\n", veins[2][2]);

return;
}

num_veins[] gives the right number, 10, but when trying to access to any elements of veins, it gives segmentation fault. num_veins []给出正确的数字10,但是当尝试访问任何静脉元素时,它给出了分割错误。 How can I use in main.c the values of veins that are filled in func2.h ? 如何在main.c中使用func2.h中填充的静脉的值? What am I doing wrong and why is this happening ? 我在做什么错,为什么会这样?

EDIT: 编辑:

These codes are a simplified version of bigger ones. 这些代码是较大代码的简化版本。 In the answers there is a way to solve the problem, but it is not exactly what I want. 在答案中,有一种解决问题的方法,但这并不是我想要的。 What I need is to do the dynamic allocation in func2.h , so I don't want any calculation involving veins[][] in main.c before the call to network_construction(). 我需要在func2.h进行动态分配,因此在调用network_construction()之前,我不想在main.c进行任何涉及静脉[] []的计算。

veins = malloc(NN * sizeof(*veins)); is your problem. 是你的问题。 You actually need an int*** to do what you want. 您实际上需要一个int***才能完成您想要的操作。 Inside network_construction , the argument veins is actually a copy of the variable veins in your main . network_construction内部,参数静脉实际上是您的main可变veins的副本。 Therefore when you malloc it, only the copy changes, but not the variable in main. 因此,当您对其进行malloc分配时,只有副本会更改,而main中的变量不会更改。

A simpler example of the same problem would be this: 相同问题的一个简单示例是:

void foo(int a){
    a=5;
}
void foo2(int* a){
    *a=5;
}
int main(){
    int a=0;
    foo(a);
    printf("%d\n",a);
    foo2(&a);
    printf("%d\n",a);
}

which prints 0 5 . 打印0 5 Your case is the same, but instead of int and int* , think of int** and int*** 您的情况是相同的,但是要考虑int**int***而不是intint*

Your function call should be as follows: 您的函数调用应如下所示:

network_construction(&veins, num_veins); network_construction(&veins,num_veins);

Then in the function network_construction catch veins as follows: 然后在函数network_construction中捕获静脉,如下所示:

void network_construction(int ***veins, int *num_veins){

        int i, j;


        for(i=0;i<NN;i++){
            num_veins[i] = NN/2;
        }


        *veins = malloc(NN * sizeof(**veins));
        for (i = 0; i < NN; i++) { *veins[i] = malloc(num_veins[i] * sizeof(**(veins[i]))); }
        for (i = 0; i < NN; i++) { for (j = 0; j<num_veins[i];j++) { *veins[i][j] = -1; } }


    return;
    }

Your malloc in network_construction() function is the problem. 您在network_construction()函数中的malloc就是问题所在。 Easy to overcome this thing is to modify your functions as follows: 容易克服的事情是按如下方式修改您的功能:

Your func.c should be as follows: 您的func.c应该如下所示:

 #define NN 20

    void network_construction(int **veins, int *num_veins){

            int i, j;

           for (i = 0; i < NN; i++) { for (j = 0; j<num_veins[i];j++) { veins[i][j] = -1; } }


        return;
        }

Your main function will be as follows: 您的主要功能如下:

#include <stdio.h>
#include <stdlib.h>
#include "func2.h"

void main(){

    int num_veins[NN];
    int **veins;
int i;


        for(i=0;i<NN;i++){
            num_veins[i] = NN/2;
        }


        veins = malloc(NN * sizeof(*veins));
        for (i = 0; i < NN; i++) { veins[i] = malloc(num_veins[i] * sizeof(*(veins[i]))); }

    network_construction(veins, num_veins);

    printf("\n%d\n", num_veins[19]);
    printf("\n%d\n", veins[2][2]);

return;
}

The code below achieves what I wanted: 下面的代码实现了我想要的:

void network_construction(int ***veins, int num_veins[]){
  int i,j;

  for(i=0;i<NN;i++){
    num_veins[i] = NN/2;
  }

  (*veins) = malloc((NN) * sizeof(**veins));

  for(i=0;i<NN;i++){
    (*veins)[i] = malloc(num_veins[i]*sizeof(*(*veins)[i]));
  }

  for (i = 0; i < NN; i++) { 
    for (j = 0; j < num_veins[i]; j++) {
      (*veins)[i][j] = -1; 
    }
  }
  return;}

To call it in main(): 要在main()中调用它:

network_construction(&veins,num_veins);

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

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