Wie der Titel schon sagt, versuche ich, pset4 zu machen, wo ich diesen Fehler bekomme, wenn Ich versuche, das Programm auszuführen. Ich habe das Forum und auch Reddit durchsucht und verstanden, dass ich ein Problem mit dem malloc () habe. Ich habe festgestellt, dass dies nicht der Fall ist, wenn ich diese Art von Fehler erhalte meinem Zeiger genügend Speicher zuweisen. Es tut mir leid, wenn diese Frage beantwortet wurde oder wenn sie zu dumm ist, aber ich bleibe an 5 aufeinander folgenden Tagen bei diesem Fehler. Hier ist mein Code und ich wäre mehr als glücklich, wenn mich jemand sehen lassen könnte Was ich falsch mache. Vielen Dank für Ihre Zeit!

 // Copies a BMP file #include <stdio.h> #include <stdlib.h> #include "bmp.h" int main(int argc, char *argv[]) { // ensure proper usage if (argc != 4) { fprintf(stderr, "Usage: copy infile outfile\n"); return 1; } // remember filenames int n = atoi(argv[1]); char *infile = argv[2]; char *outfile = argv[3]; // check if n is correct if (n < 0 || n > 100) { fprintf(stderr, "n must be a positive integer less than or equal to 100\n"); return 1; } // open input file FILE *inptr = fopen(infile, "r"); if (inptr == NULL) { fprintf(stderr, "Could not open %s.\n", infile); return 1; } // open output file FILE *outptr = fopen(outfile, "w"); if (outptr == NULL) { fclose(inptr); fprintf(stderr, "Could not create %s.\n", outfile); return 1; } // read infile"s BITMAPFILEHEADER BITMAPFILEHEADER bf; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); // read infile"s BITMAPINFOHEADER BITMAPINFOHEADER bi; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); // ensure infile is (likely) a 24-bit uncompressed BMP 4.0 if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || bi.biBitCount != 24 || bi.biCompression != 0) { fclose(outptr); fclose(inptr); fprintf(stderr, "Unsupported file format.\n"); return 2; } // determine padding for scanlines int infilePadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // update output file"s header info while keeping the old one int infileBiWidth = bi.biWidth; int infileBiHeight = bi.biHeight; bi.biWidth *= n; bi.biHeight *= n; // use malloc for creating memory to store pixels of outfile RGBTRIPLE* ppixel = malloc(sizeof(RGBTRIPLE) * bi.biWidth); // create a variable for keeping track of ppixelIndex int ppixelIndex = 0; // calculate padding bytes for output file int outfilePadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // recalculate header info bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + outfilePadding) * abs (bi.biHeight); bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); // write outfile"s BITMAPFILEHEADER fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr); // write outfile"s BITMAPINFOHEADER fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr); // iterate over infile"s scanlines for (int i = 0; i < abs(infileBiHeight); i++) { // iterate over pixels in scanline for (int j = 0; j < infileBiWidth; j++) { // temporary storage RGBTRIPLE triple; // read RGB triple from infile fread(&triple, sizeof(RGBTRIPLE), 1, inptr); // store each pixel n times to ppixel for (int l = 0; l < n; l++) { ppixel[ppixelIndex] = triple; ppixelIndex++; } } // for each pixel write it n times for (int m = 0; m < n; m++) { for (int p = 0; p < n; p++) { // write RGB triple to outfile fwrite(&ppixel[p], sizeof(RGBTRIPLE), 1, outptr); } // write outfile"s padding for (int k = 0; k < outfilePadding; k++) { fputc(0x00, outptr); } } // skip over infilePadding, if any fseek(inptr, infilePadding, SEEK_CUR); } // close infile fclose(inptr); // close outfile fclose(outptr); // free ppixel free (ppixel); // success return 0; } 

Kommentare

  • Danke @ MARS für die Unterstützung beim Format. Es war mein erster Beitrag, Entschuldigung …

Antwort

Ich glaube, ich habe die Antwort gefunden. Das Hauptproblem war, dass ich die Variable ppixelIndex aus der for-Schleife gesetzt und sie viel zu stark inkrementiert habe. Also, als die erste Iteration für die Die erste Zeile ist beendet, ich habe sie nicht zurückgesetzt, also habe ich während der zweiten Iteration versucht, den Speicher zu erreichen, der nicht zugewiesen wurde. Ich habe auch meinen Code ein wenig geändert, aber die Hauptlösung war das Verschieben der ppixelIndex Variable innerhalb der Schleife. Hoffe diese Lösung hilft jemandem 🙂

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.