在 C 中为多个测试用例返回 -11

Returned -11 in C for multiple Test Cases

提问人:Peytonsalv 提问时间:11/29/2022 更新时间:11/29/2022 访问量:24

问:

据我了解,-11 SIGSEGV 是内存错误。我通过了四个测试用例,现在对于我的提交,我失败了其中的大部分。请查看此图像以更好地了解我的问题。 在这里 请查看第二张图片,以便更好地了解我的问题。

我在项目评分中包含了两张图像。我几乎可以肯定,这是我代码中某处的错误定位错误,我尝试使用调试器,但它并没有太大帮助,我也可能关闭了错误的文件,但我认为我不是。我在下面发布的代码中会有注释,所以它应该很容易理解。

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

#include "image.h"

// open the file, create an ImagePPM, and return the pointer
// return NULL if the file cannot be opened

ImagePPM *readPPM(char *filename)
{
    FILE *inFile = NULL;
    inFile = fopen(filename,"r");
    if (inFile == NULL){
        return NULL;
    }
    //made
    
    ImagePPM *image = malloc(sizeof(struct _imagePPM));
    fscanf(inFile, "%*s");
    
    int numRows, numCols, limit = 0;
    fscanf(inFile, "%d %d %d", &numRows , &numCols, &limit);
    image -> pixels = (struct _pixel **)malloc(numRows*sizeof(struct _pixels *));
    for (int i = 0 ;i < numRows; i++){
        image->pixels[i] = (struct _pixel*)malloc(numCols*sizeof(struct _pixel));

    }
    for (int i = 0; i < numRows; i++){
        for (int j = 0; j < numCols; j++){

            int red;
            int green;
            int blue;

            fscanf(inFile, "%d %d %d", &red, &green, &blue);
            image->pixels[i][j].red =red;
            image->pixels[i][j].green =green;
            image->pixels[i][j].blue = blue;
            // removing (&(image->)
        }
    }
  
    strcpy(image->magic, "P3");
    image -> maxVal = limit;
    image -> numRows = numRows;
    image -> numCols = numCols;
    
    fclose(inFile);

return image;
}
// open the file and write the ImagePPM to the file
// return 1 on success
// return 0 if the file cannot be opened

int writePPM(ImagePPM *pImagePPM, char *filename)
{
    FILE *outFile = fopen(filename,"w");
    if (outFile == NULL){
        return 0;
    }

    fprintf(outFile, "%s\n%d\t%d\n%d\n", pImagePPM -> magic, pImagePPM ->numRows, pImagePPM -> numCols , pImagePPM -> maxVal);
    for (int i = 0; i < pImagePPM -> numRows; i++){ //might be numCols
        for (int j = 0; j < pImagePPM -> numCols; j++){ //might be numRows
            Pixel *PH = &(pImagePPM -> pixels[i][j]);
            fprintf(outFile, "%d %d %d ", PH -> red , PH -> green , PH -> blue);
            //free(Pixel *PH); maybe need to free a struct?
        }
        fprintf(outFile, "\n");
        //fprintf(outFile,"made it here");

    }
    //fclose(filename);
    fclose(outFile);


return 1;
}

// free the ImagePPM and its pixels
// everything with a malloc needs a free

void freePPM(ImagePPM *pImagePPM)
{
    int length = pImagePPM -> numCols;
    for(int i = length -1 ; i >= 0; i--){
        free(pImagePPM -> pixels[i]);
    }
    free(pImagePPM);
    return;
}

// open the file, create an ImagePGM, and return the pointer
// return NULL if the file cannot be opened

ImagePGM *readPGM(char *filename)
{

    FILE *inFile = NULL;
    inFile = fopen(filename, "r");
    if (inFile == NULL){
        return NULL;
    }
    ImagePGM *image = malloc(sizeof(struct _imagePGM));
    fscanf(inFile, "%*s");

    int numRows, numCols , limit = 0;
    fscanf(inFile, "%d %d %d", &numRows , &numCols , &limit);

    image -> pixels = (int **)malloc(numRows*sizeof(int*));
    for(int i = 0; i <numRows; i++){
        image -> pixels[i] = (int*)malloc(numCols*sizeof(int));
    }
    for (int i = 0; i < numRows; i++){
        for (int j = 0; j < numCols; j++){
            int total = 0;
            fscanf(inFile, "%d", &total);
            image -> pixels[i][j] = total;
        }
    }

    strcpy (image -> magic, "P2");
    image -> maxVal = limit;
    image -> numRows = numRows;
    image -> numCols = numCols;

    fclose(inFile);
    //fclose(filename);

    return image;
}

// open the file and write the ImagePGM to the file
// return 1 on success
// return 0 if the file cannot be opened

int writePGM(ImagePGM *pImagePGM, char *filename)
{
    FILE *outFile = NULL; 
    outFile = fopen(filename, "w");
    if (outFile == NULL){
        return 0;
    }
    fprintf(outFile, "%s\n%d\t%d\n%d\n",
    pImagePGM->magic, pImagePGM->numRows,
    pImagePGM->numCols, pImagePGM->maxVal);

    for (int i = 0 ; i < pImagePGM -> numCols; i++){
        for (int j = 0; j < pImagePGM -> numRows; j++){
            fprintf(outFile, "%d ", pImagePGM -> pixels[i][j]);
        }
        fprintf(outFile, "\n");

    }
    fclose(outFile);
    //fclose(filename);
return 1;
}

// free the ImagePGM and its pixels
// everything with a malloc needs a free

void freePGM(ImagePGM *pImagePGM)
{
    int numRows = pImagePGM -> numCols;
        for (int i = numRows -1; i >=0; i--){
            free(pImagePGM->pixels[i]);
         }
         free(pImagePGM);
    return ; // maybe NULL or zero
}

ImagePGM *convertToPGM(ImagePPM *pImagePPM)
{
    int x ,y , max;
    x = pImagePPM -> numCols;
    y = pImagePPM -> numRows;
    max = pImagePPM -> maxVal;
                                            // space error 
    ImagePGM *image = malloc(sizeof(struct _imagePPM));
    image -> pixels = (int **)malloc(x*sizeof(int*));
    for (int i = 0; i < x; i++){
        image -> pixels[i] = (int*)malloc(y*sizeof(int));
        // error for struct needs to be fixed


    }
    strcpy(image -> magic, "P2");
    image -> numRows = x;
    image -> numCols = y;
    image -> maxVal = max;

    for (int i = 0; i < x; i++){
        for (int j = 0; j < y; j++){
            int total = 0;
            total += pImagePPM->pixels[i][j].red;
            total += pImagePPM->pixels[i][j].green;
            total += pImagePPM->pixels[i][j].blue;
            total /=3; // converting to grey scale by / itself and 3
            image -> pixels [i][j] = total;

        }
    }
    return image;
}

ImagePGM *shrinkPGM(ImagePGM *pImagePGM)
{
    int oldRow , oldCol, x, y, max;
    oldRow = pImagePGM -> numRows;
    oldCol = pImagePGM -> numCols;
    x = oldRow/2;
    y = oldCol/2;
    max = pImagePGM -> maxVal;
    ///////////
    //printf("Made it here");

    ImagePGM *image = malloc(sizeof(struct _imagePPM));
    image -> pixels = (int **)malloc(x*sizeof(int*));
    for (int i = 0; i < x; i++){
        image -> pixels[i] = (int*)malloc(y*sizeof(int));

    }
    
    strcpy(image -> magic, "P2");
    image -> numRows = x;
    image -> numCols = y;
    image -> maxVal = max; // could be limit instead of max or max val


    for(int i = 0; i < x; i++){
        for(int j = 0; j < y; j++){
            int total = 0;
            total += pImagePGM->pixels[i*2][j*2];
            total += pImagePGM->pixels[i*2][j*2+1];
            total += pImagePGM->pixels[i*2+1][j*2];
            total += pImagePGM->pixels[i*2+1][j*2+1];
            total /= 4;
            image->pixels[i][j] = total;
        }
    }
    return image;
}

起初,我的测试用例失败了,然后使用了我从堆栈溢出中得到的一些答案。例如将我的一个 for 循环将 i++ 更改为 i--,然后我尝试提交我的项目并被赋予了许多返回 -11 状态。我对从这里何去何从感到非常困惑。

C 分段-断层 Malloc 项目

评论


答: 暂无答案