1

我的程序在 WinXp 的 MINGW32 上运行。该程序由gcc编译并使用gdb调试工具得到预期的结果。但是在MINGW32下直接运行时,WinXp会报错:

“0x7c956a6e”指令引用了“0x00000000”的内存。该内存无法“读取”。

那么如何定位这条指令呢?我已经检查过了,它似乎是由 malloc 函数引起的。源码如下:</p>

int find(FILE *fp, char **head) {
    char str[100];
    char *p, c;
    int i = 0, j = 0;

    do {
        c = fgetc(fp);
        if(c == EOF)
            return 0;
    } while(c == ' ' || c == '\n');

    while(c != ' ' && c != '\n') {
        str[i++] = c;
        c = fgetc(fp);
    } 

    *head = (char *)malloc(sizeof(char) * i);
    if(*head == NULL)
        printf("\nCannot get memory (find)");
    else {
        p = *head;
        for(j = 0; j < i; j++) {
            *(p++) = str[j];
        }
        *p = '\0';
        return 1;
    }
    return 0;
}

如果有人可以提供帮助,我们将不胜感激!

10/7 感谢您的评论。这是其余的代码和数据模式:

int main() 
{
    FILE *fp;
    int lines = 0,  i = 0;
    TianYa *yp, *linep;
    char *head = NULL;

    fp = fopen("tysample.txt", "r");

    if(fp == NULL)
        printf("\nCannot open file");
    else {
        yp = (TianYa *)malloc(TIAN_YA_LINES * sizeof(TianYa));
        if(yp == NULL)
            printf("\nCannot get memory (main)");
        else {
            linep = yp;
            i = 0;
            while(find(fp, &head) != 0) {
                switch(i++) {
                    case 0:
                        linep->name = head;
                        break;
                    case 1:
                        linep->passwd = head;
                        break;
                    case 2:
                        linep->email = head;
                        i = 0;
                        linep++;
                        lines++;
                        break;
                }
            }
        }
    }
    linep = yp;
    for(i = 0; i < lines; i++) {
        printf("\nName:%s, Passwd:%s, Email:%s", linep->name, linep->passwd, linep->email);
/*      free(linep->name);
        free(linep->passwd);
        free(linep->email);
*/      linep++;
    }

    free(yp);
    yp = NULL;
    linep = NULL;

    if(fclose(fp))
        printf("\nFailed to close file");

    return 0;
}


momre           123456           baitianyang@live.cn
白天也懂           123456           baitianyedong@tianya.cn
baitiansosuo           weirenminfuwu           baitianyouxiang@sohu.com
heng__chang           87732351           baitianyu.06@163.com
风正                           5581100           baitianyu@126.com
sparkbai           20001228           baitianyu@tianya.cn
背粪篓进银行           19851019           baitianyu123@163.com
白天月           11200929           baitianyue@126.com
4

0 回答 0