c - C 中的段错误(代码转储)错误

标签 c file terminal segmentation-fault void

我试图运行此代码,它显示“段错误(代码已转储)”,我如何修复我的代码以使此错误消失。这是我的代码,所以如果有人可以帮助我,那就太棒了!我该如何修复无效的搜索

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct _data {
    char *name;
    long number;
};
int SCAN(FILE *(*stream)){
    int count = 0;
    char line[256];
    while (fgets(line, sizeof(line), *stream)) {
        count++;
    }
    return count;
}
struct _data*  BlackBoxLOAD(FILE **stream, int size){
    struct _data* BlackBox = (struct _data*)malloc(sizeof(struct _data)*size);
    int count = 0;
    char line[256];
    while (fgets(line, sizeof(line), *stream)) {
        char* token = strtok(line, " ");
        struct _data* temp = (struct _data*)malloc(sizeof(struct _data));
        temp->name = token;
        token = strtok(NULL, " ");
        temp->number = atoi(token);
        BlackBox[count] = *temp;
        count++;
    }
    return BlackBox;
}
void SEARCH(struct _data *BlackBox, char *string, int size){
    int i = 0;
    for (i = 0; i<size; i++){
        if (strcmp(BlackBox[i].name, string) == 0)
            return i;
    }
    return -1;
}
void FREE(struct _data *BlackBox, int size){
    free(BlackBox);
    return;
}
int main(int argc, char **argv) {
    int i = 0, size = 0;
    FILE *stream = fopen("hw5.data", "r");
    int noOfLines = SCAN(&stream);
    size = noOfLines;
    struct _data *BlackBox = BlackBoxLOAD(&stream, size);
    fclose(stream);
    for (i = 1; i<argc; i++){
        if (argv[i] == "") {
            printf("*******************************************");
            printf("* You must include a name to search for. *");
            printf("*******************************************");
        }
        int pos = SEARCH(BlackBox, argv[i], size);
        if (pos == -1) {
            printf("*******************************************");
            printf("The name was NOT found.");
            printf("*******************************************");
        }
        else{
            printf("*******************************************");
            printf("The name was found at the %d entry.", pos);
            printf("*******************************************");
        }
    }
    FREE(BlackBox, size);
}

最佳答案

这段代码中有太多错误,但这是最明显的:

    char* token = strtok(line, " ");
    struct _data* temp = (struct _data*)malloc(sizeof(struct _data)*1000);
    temp->name = token;

您将 temp->name 设置为 token 的值。但是 token 指向 line,它正在被修改,并且很快就会不复存在,因为您在即将消失的堆栈上创建了它。

您无法保存指向要重用的内存块的指针。

关于c - C 中的段错误(代码转储)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28820594/

相关文章:

java - 使用 JAVA/shellscripts 执行 sudo 命令

c - 如何使用c程序判断用户输入的命令是否存在于linux中

c - free 的参数 1 的类型不兼容

c - 在 C99 : how to access a specific byte within a long double variable

c - 长度大于8192的base64解码文件?

linux - 在Linux中获取特定目录中的文件数

php - 密码保护目录,但仍允许在主页上使用

java - 使用 java.io.File 在文件系统上创建文件

javascript - 为什么 npm install 没有将包安装到/node_modules 文件夹中?

xcode - Mac终端返回 "xcrun: error: unable to exec Xcode native xcrun (Exec format error)."