Tak jak mówi tytuł, próbuję wykonać pset4, w którym pojawia się ten błąd, gdy Próbuję uruchomić program. Przeszukałem forum, a także Reddit i zrozumiałem, że mam problem z malloc (). Odkryłem, że kiedy otrzymuję tego typu błąd, oznacza to, że nie jestem przydzielanie wystarczającej ilości pamięci mojemu wskaźnikowi. Przepraszam, jeśli udzielono odpowiedzi na to pytanie lub jest zbyt głupie, ale utknąłem na tym błędzie przez 5 kolejnych dni. Więc oto mój kod i byłbym bardziej niż szczęśliwy, gdyby ktoś mógł sprawić, żebym zobaczył co robię źle. Dziękuję za poświęcony czas!

 // 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; } 

Komentarze

  • Dzięki @ MARS za pomoc przy formatowaniu. To był mój pierwszy post, przepraszam …

Odpowiedź

Myślę, że znalazłem odpowiedź. Główny problem polegał na tym, że ustawiałem zmienną ppixelIndex z pętli for i zwiększałem ją o wiele za bardzo. Kiedy więc pierwsza iteracja dla kończy się pierwszy wiersz, nie resetowałem go, więc podczas drugiej iteracji próbowałem dotrzeć do pamięci, która nie została przydzielona. Zmieniłem też trochę kod, ale głównym rozwiązaniem było przeniesienie do wnętrza pętli. Mam nadzieję, że to rozwiązanie komuś pomoże 🙂

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *