我不知道为什么这个程序在Ubuntu上执行得很好,但在debian系统上却没有。当我在x和y中调用双for()
时,我遇到了分段错误。
如果我对双重评论,一切都好。如果我不评论,我得到分段错误。此外,如果您评论所有内容,它也会出现分段错误。按顺序输入数据:128,0.5,0
修改 我向所有读过这个不完整主题的人道歉。我写得太快了。我们来谈谈这段代码吧。想象一下,你有一个LxL尺寸的格子。在每个点(x,y),我们都有一个呼叫中心。呼叫中心也由两个变量定义:第一个是他的状态。在线或离线。第二个,标签。这就是使用我称为 site 的结构的原因。每个站点都连接到他的邻居,特别是每个站点都有4个邻居:顶部,右侧,底部,左侧(也在边界上)。想象一下,我们的呼叫中心的rho密度被破坏了,或者呼叫中心工作的rho密度也是如此。现在我们使用工作/不工作呼叫中心填充LxL晶格。如果有人试图从右上角呼叫到左下角,左下角被破坏的呼叫中心隔离会发生什么?该程序试图模拟集群连接和传播。它从该区域的总体开始,然后使用 update 函数,为每个点验证邻居是否具有相同的状态并传播较低的标签。当程序结束时,我们可以找到许多具有相同标签的集群,并检查集群是如何渗透的。
我的代码。
#include <stdio.h>
#include <stdlib.h>
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define BLD "\x1B[1m"
#define RST "\x1B[0m"
#define ONLINE 1
#define OFFLINE 0
struct site {
int status;
int label;
} typedef site;
void createDynamicArray(site ***array, int L, int N);
void updateLabel(site ***node, int x, int y, int nx, int ny);
int main(void) {
int L, seed;
double rho;
fprintf(stderr, "\nRetrieving data from input file...... ");
fflush(stderr);
scanf("%d\n%lf\n%d", &L, &rho, &seed);
fprintf(stderr, "[%s%sDONE%s]\n", BLD, GRN, RST);
// Eseguo alcuni controlli sui dati iniziali;
if (rho == 1) {
fprintf(stderr, "[%s%sFAILED%s] Can't start simulation with rho = 1;\n", BLD, RED, RST);
exit(EXIT_FAILURE);
}
// L'utente non ha fornito un seme specifico, verra' preso da /dev/urandom;
if (seed == 0) {
FILE *fp = fopen("/dev/urandom", "r");
fread(&seed, 1, sizeof(unsigned int), fp);
fclose(fp);
}
// Dichiaro l'array di struct;
site ***node;
createDynamicArray(node, L, L);
// Popolo il reticolo;
int x, y;
double r;
srand48(seed);
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
r = drand48();
(*node)[x][y].label = x + L*y;
if (r < rho) {
(*node)[x][y].status = ONLINE;
} else {
(*node)[x][y].status = OFFLINE;
}
//fprintf(stderr, "%d\t", (*node)[x][y].label);
}
//fprintf(stderr, "\n");
}
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
updateLabel(node, x, y, (x+1)%L, y); // Destra;
updateLabel(node, x, y, (x + L - 1)%L, y); // Sinistra;
updateLabel(node, x, y, x, (y+1)%L); // Alto;
updateLabel(node, x, y, x, (y+L-1)%L); // Basso;
}
}
//FILE *fp_out = fopen("output.dat", "w");
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
fprintf(stdout, "%d\t%d\t%d\t%d\n", x, y, (*node)[x][y].label, (*node)[x][y].status);
}
}
//fclose(fp_out);
/*fprintf(stderr, "\n\n");
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
fprintf(stderr, "%d\t", (*node)[x][y].status);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n\n");
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
fprintf(stderr, "%d\t", (*node)[x][y].label);
}
fprintf(stderr, "\n");
}*/
// Libero gli array;
return 0;
}
void createDynamicArray(site ***array, int L, int N) {
int i;
*(array) = (site **)calloc(L, sizeof(site *));
if (*(array) == NULL) {
fprintf(stderr, "[%s%sFAILED%s] Allocazione dinamica di memoria di (array) NON riuscita.\n", BLD, RED, RST);
exit(EXIT_FAILURE);
}
for (i=0; i<L; i++) {
(*array)[i] = (site *)calloc(N, sizeof(site));
// Verifica della corretta allocazione dinamica di memoria dell'i-esimo elemento di array;
if ((*array)[i] == NULL) {
fprintf(stderr, "[%s%sFAILED%s] Allocazione dinamica di memoria di (*array[%d]) NON riuscita.\n", BLD, RED, RST, i);
exit(EXIT_FAILURE);
}
}
}
void updateLabel(site ***node, int x, int y, int nx, int ny) {
// Se il vicino ha lo stesso status, posso propagare il label
if ((*node)[x][y].status == (*node)[nx][ny].status) {
if ((*node)[x][y].label < (*node)[nx][ny].label) {
(*node)[nx][ny].label = (*node)[x][y].label;
} else {
(*node)[x][y].label = (*node)[nx][ny].label;
}
}
}
答案 0 :(得分:1)
此行是导致seg故障事件的问题的开始
*(array) = (site **)calloc(L, sizeof(site *));
因为传入的参数site ***array,
包含一些随机值
建议:改变:
site ***node;
createDynamicArray(node, L, L);
到此:
site **node = NULL;
createDynamicArray(&node, L, L);
然后传递一个固定值:node
变量的地址
然后更正编译器将引发的新警告/错误。
结果是:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define BLD "\x1B[1m"
#define RST "\x1B[0m"
#define ONLINE 1
#define OFFLINE 0
typedef struct site
{
int status;
int label;
} site;
void createDynamicArray(site ***array, int L, int N);
void updateLabel(site **node, int x, int y, int nx, int ny);
int main(void)
{
int L, seed;
double rho;
fprintf(stderr, "\nRetrieving data from input file...... ");
fflush(stderr);
scanf("%d\n%lf\n%d", &L, &rho, &seed);
fprintf(stderr, "[%s%sDONE%s]\n", BLD, GRN, RST);
// Eseguo alcuni controlli sui dati iniziali;
if (rho == 1)
{
fprintf(stderr, "[%s%sFAILED%s] Can't start simulation with rho = 1;\n", BLD, RED, RST);
exit(EXIT_FAILURE);
}
// L'utente non ha fornito un seme specifico, verra' preso da /dev/urandom;
if (seed == 0)
{
FILE *fp = fopen("/dev/urandom", "r");
fread(&seed, 1, sizeof(unsigned int), fp);
fclose(fp);
}
// Dichiaro l'array di struct;
site **node = NULL;
createDynamicArray(&node, L, L);
// Popolo il reticolo;
int x, y;
double r;
srand48(seed);
for (x=0; x<L; x++)
{
for (y=0; y<L; y++)
{
r = drand48();
(node)[x][y].label = x + L*y;
if (r < rho)
{
(node)[x][y].status = ONLINE;
}
else
{
(node)[x][y].status = OFFLINE;
}
//fprintf(stderr, "%d\t", (*node)[x][y].label);
}
//fprintf(stderr, "\n");
}
for (x=0; x<L; x++)
{
for (y=0; y<L; y++)
{
updateLabel(node, x, y, (x+1)%L, y); // Destra;
updateLabel(node, x, y, (x + L - 1)%L, y); // Sinistra;
updateLabel(node, x, y, x, (y+1)%L); // Alto;
updateLabel(node, x, y, x, (y+L-1)%L); // Basso;
}
}
//FILE *fp_out = fopen("output.dat", "w");
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
fprintf(stdout, "%d\t%d\t%d\t%d\n", x, y, (node)[x][y].label, (node)[x][y].status);
}
}
//fclose(fp_out);
/*fprintf(stderr, "\n\n");
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
fprintf(stderr, "%d\t", (*node)[x][y].status);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n\n");
for (x=0; x<L; x++) {
for (y=0; y<L; y++) {
fprintf(stderr, "%d\t", (*node)[x][y].label);
}
fprintf(stderr, "\n");
}*/
// Libero gli array;
return 0;
}
void createDynamicArray(site ***array, int L, int N) {
int i;
*(array) = calloc(L, sizeof(site *));
if (*(array) == NULL) {
fprintf(stderr, "[%s%sFAILED%s] Allocazione dinamica di memoria di (array) NON riuscita.\n", BLD, RED, RST);
exit(EXIT_FAILURE);
}
for (i=0; i<L; i++) {
(*array)[i] = calloc(N, sizeof(site));
// Verifica della corretta allocazione dinamica di memoria dell'i-esimo elemento di array;
if ((*array)[i] == NULL) {
fprintf(stderr, "[%s%sFAILED%s] Allocazione dinamica di memoria di (*array[%d]) NON riuscita.\n", BLD, RED, RST, i);
exit(EXIT_FAILURE);
}
}
}
void updateLabel(site **node, int x, int y, int nx, int ny) {
// Se il vicino ha lo stesso status, posso propagare il label
if ((node)[x][y].status == (node)[nx][ny].status) {
if ((node)[x][y].label < (node)[nx][ny].label) {
(node)[nx][ny].label = (node)[x][y].label;
} else {
node[x][y].label = node[nx][ny].label;
}
}
}
编译干净,当用户输入10 10 10
时将输出;
Retrieving data from input file...... 10 10 10
[DONE]
0 0 0 1
0 1 0 1
0 2 0 1
0 3 0 1
0 4 0 1
0 5 0 1
0 6 0 1
0 7 0 1
0 8 0 1
0 9 0 1
1 0 0 1
1 1 0 1
1 2 0 1
1 3 0 1
1 4 0 1
1 5 0 1
1 6 0 1
1 7 0 1
1 8 0 1
1 9 0 1
2 0 0 1
2 1 0 1
2 2 0 1
2 3 0 1
2 4 0 1
2 5 0 1
2 6 0 1
2 7 0 1
2 8 0 1
2 9 0 1
3 0 0 1
3 1 0 1
3 2 0 1
3 3 0 1
3 4 0 1
3 5 0 1
3 6 0 1
3 7 0 1
3 8 0 1
3 9 0 1
4 0 0 1
4 1 0 1
4 2 0 1
4 3 0 1
4 4 0 1
4 5 0 1
4 6 0 1
4 7 0 1
4 8 0 1
4 9 0 1
5 0 0 1
5 1 0 1
5 2 0 1
5 3 0 1
5 4 0 1
5 5 0 1
5 6 0 1
5 7 0 1
5 8 0 1
5 9 0 1
6 0 0 1
6 1 0 1
6 2 0 1
6 3 0 1
6 4 0 1
6 5 0 1
6 6 0 1
6 7 0 1
6 8 0 1
6 9 0 1
7 0 0 1
7 1 0 1
7 2 0 1
7 3 0 1
7 4 0 1
7 5 0 1
7 6 0 1
7 7 0 1
7 8 0 1
7 9 0 1
8 0 0 1
8 1 0 1
8 2 0 1
8 3 0 1
8 4 0 1
8 5 0 1
8 6 0 1
8 7 0 1
8 8 0 1
8 9 0 1
9 0 0 1
9 1 0 1
9 2 0 1
9 3 0 1
9 4 0 1
9 5 0 1
9 6 0 1
9 7 0 1
9 8 0 1
9 9 0 1