I'm programming in C, and have two files, 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 :
#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. How can I use in main.c the values of veins that are filled in 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().
veins = malloc(NN * sizeof(*veins));
is your problem. You actually need an int***
to do what you want. Inside network_construction
, the argument veins is actually a copy of the variable veins
in your main
. Therefore when you malloc it, only the copy changes, but not the variable in 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
. Your case is the same, but instead of int
and int*
, think of int**
and int***
Your function call should be as follows:
network_construction(&veins, num_veins);
Then in the function network_construction catch veins as follows:
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. Easy to overcome this thing is to modify your functions as follows:
Your func.c should be as follows:
#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():
network_construction(&veins,num_veins);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.