0

我对这段代码有一个非常奇怪的问题,抱歉它很乱。基本上它是一个pagerank算法。每个结构网页都包含在动态数组“pages”中。pages向量通过算法直到它的绝对值(| P|) 小于 'epsilon'。现在问题出在第 195-201 行。如果我在这些行中删除对数组的迭代(即一个空的while循环),它适用于只需要一次迭代的情况。但是,当我确实有 for 循环(即使是一次迭代情况)时,它会抛出 error6(第 179 行,调试显示 e == NULL),甚至没有运行插入的循环。我已经设置了断点等,甚至在没有阅读额外代码的情况下仍然给出错误6。这里发生了什么?我对 C 和并行编程很陌生,所以它可能是基本的东西。将不胜感激任何帮助!

输入格式:

number_of_cores
number_of_pages
...
page_names
...
page_links

输出格式:

...
page_rank
...

代码

#include <assert.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const double D = 0.85;
static const double EPSILON = 0.005;
int ncores;
int npages;
struct webpage** pages;
int maxdepth;

struct webpage* has(char s[20], int e);
void* threadf(void* ptr);
int quit(void);
double rec(int s, int f, int depth);




struct webpage {
    char name[20];
    double oldrank;
    double rank;
    struct node* in;
    int incount;
    int outcount;

};

struct node {
    struct webpage* data;
    struct node* next;
};

struct arg {
    int s;
    int f;
    int depth;
    double ret;
};

struct webpage*
has(char s[20], int e) {
    int p;
    for (p=0; p<e; ++p) {
        if (strcmp(s, pages[p]->name) == 0) {
            return pages[p];
        }
    }
    return NULL;
}

void *
threadf(void* ptr) {
    struct arg* curr = (struct arg*)ptr;
    curr->ret = rec(curr->s, curr->f, curr->depth);
}
int
quit(void) {
    int i;
    for(i=0; i<npages; ++i) {
        struct node* curr = pages[i]->in;
        struct node* next;
        while(curr != NULL) {
            next = curr->next;
            free(curr);
            curr = next;
        }
        free(pages[i]);
    }
    free(pages);
    return 0;
}

double 
seq(int s, int f) {
    double sum;
    sum = 0;
    int w;
    for (w=s; w<=f; w++) {
        struct webpage* curr = pages[w];
        double ser;
        ser = 0;
        struct node* currn = curr->in;
        while (currn != NULL) {
            struct webpage* n = currn->data;
            ser = ser + ((n->oldrank)/(n->outcount));
            currn = currn->next;
        }

        double temp = (((1-D)/npages) + (D*ser)); 
        sum = sum + pow((temp - curr->oldrank), 2);
        curr->oldrank = curr->rank;
        curr->rank = temp;
    }
    return sum;
}


double 
rec(int s, int f, int depth) {
    if (depth == maxdepth ) {
        return seq(s, f);
    } else {
        if (s < f){
            int m;
            m = (s+f)/2;
            struct arg l;
            struct arg r;
            l.s = s;
            l.f = m;
            l.depth = depth+1;
            r.s = m+1;
            r.f = f;
            r.depth = depth+1;
            pthread_t left, right;
            pthread_create(&left, NULL, threadf, (void*) &l);
            pthread_create(&right, NULL, threadf, (void*) &r);
            pthread_join(left, NULL);
            pthread_join(right, NULL);
            double res;
            res = l.ret + r.ret;
            return res;
        } 
        return seq(s, f);

    }
}

int
main(void) {
    if (scanf("%d", &ncores) != 1) {
        printf("error1\n");
        return quit();
    }
    if (scanf(" %d", &npages) != 1) {
        printf("error2\n");
        return quit();
    }
    int i;
    char n[20];
    pages = (struct webpage**)malloc(npages*sizeof(struct webpage*));
    for (i=0; i<npages; ++i) {

        if (scanf(" %c", n) != 1 || has(n, i) != NULL) {
            printf("error3\n");
            return quit();
        }
        pages[i] = (struct webpage*)malloc(sizeof(struct webpage));
        struct webpage* curr = pages[i];
        strcpy(curr->name, n);
        curr->oldrank = 1/npages;
        curr->in = NULL;
        curr->incount = 0;
        curr->outcount = 0;

    }

    int nedges;
    if (scanf(" %d", &nedges) != 1) {
        printf("error4\n");
        return quit();
    }
    for (i=0; i<nedges; ++i) {
        char f[20], t[20];
        if (scanf(" %s %s", f, t) != 2) {
            printf("error5\n"); 
            return quit();
        }
        char from[20], to[20];
        strcpy(from, f);
        strcpy(to, t);
        struct webpage* s = has(from, npages);
        struct webpage* e = has(to, npages);
        if (s == NULL || e == NULL) {
            printf("error6\n");
            return quit();
        }
        s->outcount++;
        e->incount++;
        struct node* new;
        new = (struct node*)malloc(sizeof(struct node));
        new->data = s;
        if (e->in == NULL) {
            e->in = new;
        } else {
            new->next = e->in;
            e->in = new;
        }
    }
    maxdepth = (log(ncores))/(log(2)) + 0.5;
    while (sqrt(rec(0, npages-1, 0)) > EPSILON){
        int c;
        for (c=0; c<npages; ++c) {
            struct webpage* curr = pages[c];
            curr->oldrank = curr->rank;
        }
    }
    int z;
    for (z=0; z<npages; ++z) {
        struct webpage* curr = pages[z];
        printf("%s %.4lf\n", curr->name, curr->rank);
    }

    return quit();

}

样本输入:

8
4
a
b
c
d
4
a a

输出:

error6
4

1 回答 1

1
char n[20];
[ ... ]
    if (scanf(" %c", n) != 1 || has(n, i) != NULL) {

%c格式说明符scanf只读取一个字符。所以n包括你输入的字符加上你调用之前在堆栈上发生的任何垃圾scanf()。如果您使用%s,它将由您键入的字符加上一个用于终止字符串的 NUL 字节以及您不关心的垃圾组成。

另请注意,您可以scanf()使用宽度说明符来限制读取的字符数量,如下所示:

scanf("%19s", n)

(意思是:读取 19 个字符并添加一个 NUL 字节)。否则,您的缓冲区可能会溢出,可能导致任意代码执行(或至少在非恶意用户使用时崩溃)。

于 2011-05-14T08:51:12.493 回答