【问题标题】:Getting a segmentation fault in C, dont know why? [closed]在 C 中出现分段错误,不知道为什么? [关闭]
【发布时间】:2020-11-04 22:34:51
【问题描述】:

我也使用过 Valgrind,但仍然找不到错误。恢复图像是 CS50 问题。

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if ( argc != 2)
    {
        printf("Usage : ./recover image\n");
        return 1;
    }
    //Condition to check whether the file opens or not.
    FILE *file = fopen(argv[1], "r");
    int c=0;
    FILE *img;
    char *fileName = malloc(sizeof(char)*10);
    if(fileName == NULL)
    return 1;
        //Below is the dynamic reading of file.
    do
    {
        int *arr = malloc(sizeof(BYTE)*512);
        if(arr == NULL)
        return 1;         
        fread(arr,sizeof(BYTE),512,file);
        if(arr [0] == 0xff && arr[1] == 0xd8 && arr[2] == 0xff && (arr[3] & 0xf0) == 0xe0)
        {
            if(c == 0)
            {
                img = fopen(" 000.jpg","a");// check for w or a
                fwrite(arr,sizeof(BYTE),512,img);
            }
            else 
            {
                fclose(img);
                sprintf(fileName,"%03i.jpg",c);
                img = fopen(fileName,"a");//check for w or a
                fwrite(arr,sizeof(BYTE),512,img);
            }
            c++;
        }
        else 
        {
            if(c!=0)
            {
                img = fopen(fileName,"a");
                fwrite(arr,sizeof(BYTE),512,img);
            }
        }
            free(arr);
    }while(getc(file) != EOF);
    if(img != NULL)
        fclose(file);
    fclose(img);
    free(fileName);
}

screenshot with error message

【问题讨论】:

  • " 一切都无关紧要" - 这不是你写好问题的方式
  • 你试过用gdb之类的调试器吗?
  • 使用 valgrind 查找内存泄漏,使用 gdb 之类的调试器并读取回溯以解决运行时错误
  • OT:紧随其后:FILE *file = fopen(argv[1], "r"); 应该是检查错误,而不是稍后的某些语句。所以在这个语句之后应该是:if( ! file ) { perror( "fopen for input file failed" ); exit( EXIT_FAILURE ); } 这输出到stderr,你的错误信息和系统认为发生错误的文本原因。
  • 关于:int *arr = malloc(sizeof(BYTE)*512); if(arr == NULL) return 1; 这没有通知用户发生了问题,也没有通知用户有关问题的详细信息。建议:int *arr = malloc(sizeof(BYTE)*512); if(arr == NULL) { perror( "malloc failed" ); fclose( file ); exit( EXIT_FAILURE ); } 注意:表达式:sizeof( char ) 在 C 标准中定义为 1。将任何内容乘以 1 无效,只会使代码混乱。建议删除表达式:sizeof(BYTE)

标签: c computer-science cs50


【解决方案1】:

您的问题是您的代码很糟糕。您缺少的是错误检查。大多数库函数(如fopenmalloc 等)通过返回值指示是否成功。例如,如果您要打开的文件不存在,fopen 可能会失败。

请参阅fopen documentation

fopen()、fdopen() 和 freopen() 成功完成后返回一个 文件指针。否则,返回 NULL 并将 errno 设置为 指出错误。

对于代码中的每个库调用,请考虑如果函数失败会发生什么。你需要考虑这种情况。您至少应该打印一条错误消息并正确退出程序。

例如对于fopen

*file = fopen(argv[1], "r");
if (file == NULL) {
    fprintf(stderr, "fopen() failed: %s\n", strerror(errno));
    return 1;
}
// ...

编辑:如果你想成为一名软件开发人员,你应该研究这些事情:

【讨论】:

    【解决方案2】:

    你在你的代码中检查过这个吗?

    int *arr = malloc(sizeof(BYTE)*512);
    

    我看不出你在哪里释放它。试试这个,让我知道,我可以帮忙

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多