C分段错误11

总督

我在USACO上写小程序有关问题。

问题被发布为http://usacotraining.blogspot.co.uk/2013/09/problem-112-greedy-gift-givers.html

这是我的代码:

    /*
ID: xin.sun2
LANG: C
TASK: gift1
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int searchIndex(char *list[], char* s, int size) {
    for (int i = 0; i < size; i ++) {
        if (strcmp(list[i], s) == 0) {
            return i;
        }   
    }
    return -1;
}

int main() {

    freopen("gift1.in", "r", stdin);
    freopen("gift1.out", "w", stdout);

    int num_people = 0;
    scanf("%d", &num_people);

    char *nameList[num_people] = malloc(num_people*100*sizeof(char));
    int money[num_people];
    memset(money, 0, num_people*sizeof(int));

    for (int i = 0; i < num_people; i++) 
        scanf("%s", nameList[i]);

    char* this_people = malloc(100*sizeof(char));
    while (scanf("%s", this_people) != EOF) {
        int amount, num_giving, people_index;
        people_index = searchIndex(nameList, this_people, num_people);
        scanf("%d", &amount);
        scanf("%d", &num_giving);
        money[people_index] -= amount/num_giving*num_giving;
        int giving_amount = amount/num_giving;
        for (int i = 0; i < num_giving; i++) {
            char* p = malloc(100*sizeof(char));
            scanf("%s",p);
            int this_index = searchIndex(nameList, p, num_people);
            money[this_index] += giving_amount;
        }
    }

    for (int i = 0; i < num_people; i++) {
        printf("%s %d\n", nameList[i], money[i]);
    }

    return 0;
}

当我编译.c文件时,但是当我执行文件时,它没有问题。它显示了分段错误11。我确实尝试使用gdb,结果是

Program received signal SIGSEGV, Segmentation fault.
0x00007fff96b17908 in ?? ()

我有两个问题:1.我的代码(算法)有什么问题?这可能是语法问题,因为我真的是c编程2的新手。为什么gdb在问题所在的main中显示??()而不是行号。我是否以错误的方式使用gdb?

谢谢大家!

更新

我已经纠正了有关不对数组和char进行符号空间签名的问题。但是有一个编译错误

null.c:27:8: error: variable-sized object may not be initialized
        char *nameList[num_people] = malloc(num_people*100*sizeof(char));

我对初始化字符串数组的正确方法以及如何将字符串数组作为参数传递给函数有疑问。

贡特拉姆·布洛姆(Guntram Blohm)
char* this_people;
while (scanf("%s", this_people) != EOF) {

char* p;
scanf("%s",p);

是错误的,this_people和p指向无处,而scanf使用此“无处”作为输入缓冲区。使用malloc()分配一些内存,或将p设置为数组。(如果用户输入的内容长于您提供的空间,您仍然会遇到问题,但是在您学习时,直到稍后才出现问题)。

当您遇到这样的问题时,gdb命令bt就是您的朋友。它显示崩溃后从何处调用了哪些函数。

对于名称列表数组,您要使用:

定义:

char **namelist;
namelist=malloc(num_people*sizeof (char *));

或,但这需要“新”编译器,因为它不在旧的C规范中:

char *namelist[num_people];

然后,在这两种情况下:

for (i=0; i<num_people; i++)
    namelist[i]=malloc(100);

(您应该检查malloc是否返回NULL并抛出一个错误,但是为了简洁起见,我省略了它)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章