#include stdio.h
#include stdlib.h
unsigned int Node_size;
float Mean_degree;
int (*bin)[2] , i , j , k=0 , l=0;
printf("Node size :?");
scanf("%d",&Node_size);
printf("Mean degree :?");
scanf("%f",&Mean_degree);
bin=(int(*)[2])malloc(sizeof(int)*Node_size*(Node_size-1));
for(i=0;i<Node_size;i++){
for(j=i+1;j<Node_size;j++){
bin[k][l]=i;
l++;
bin[k][l]=j;
l--;
k++;
}
}
for (i=0;i<Node_size*(Node_size-1);i++){
printf("%d ",*(*(bin)++));
}
for(i=0;i<Node_size*(Node_size-1);i++){
free(bin[i]);
}
free(bin);
bin[Node_size*(Node_size-1)][2]
was dynamically assigned and used as a two-dimensional array.
At first, only one free(bin)
was used, but a problem occurred, so free(bin[i])
was added.
Still, there was a problem, so I want to know where the wrong code was used.
Thank you.
c dynamic-allocation
int (*bin)[2] ?
Why did you do that? Normal
int bin[][2];
I declare it this way
int *bin[2];
Like this
And you need to allocate memory A two-dimensional pointer is an address that should be assigned to each one, but only one has been assigned.
for (i=0;i<Node_size*(Node_size-1);i++)
bin[i]=(int*)malloc(sizeof(int));
Please assign it as above
© 2024 OneMinuteCode. All rights reserved.