C Speicherzuordnung: Warum ist nicht genügend Speicher vorhanden (nur 250 KB)?

Ich habe Probleme, den Grund herauszufinden, warum mein .c-Code Probleme hat, ~ 250K Speicher zuzuweisen. Hier ist der Zuordnungscode:

struct IMAGE {
    int width, height, maxval;
    char **data;
};

void raiseError(char *msg)
{
    printf("%s", msg);
    getch();
    exit(1);
}

//...

IMAGE readPGM()
{
    IMAGE image;
    image.data = (char **) malloc(sizeof(char)*image.height);

    //..

    for (i=0; i<image.height; i++) {
        image.data[i] = (char *) malloc(sizeof(char)*image.width);
        if (image.data[i]=='\0') {
            printf("%d\n", i);
            raiseError("Not enough memory!..");
        }
    }

    //..
}

//..

Das Programm wird beendet, wenn i = 116 ist. image.width und image.height entsprechen hier 500, daher möchte ich, dass hier 500x500 = 250000 Bytes zugewiesen werden. Es werden jedoch maximal 116x500 = 58000 Bytes zugewiesen. Gibt es also etwas, das dies einschränkt? Stimmt etwas mit meinem Code nicht? Ich poste die vollständige Quelle unten, nur für den Fall, dass es notwendig ist. Die Idee ist, eine PGM-Datei in die Struktur IMAGE einzulesen, zu verarbeiten und in eine andere Datei umzuschreiben. Wie Sie sehen können, ist es noch nicht vollständig, da ich keine Möglichkeit gefunden habe, mehr Speicher zuzuweisen.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<alloc.h>
struct IMAGE {
    int width, height, maxval;
    char **data;
};

void raiseError(char *msg)
{
    printf("%s", msg);
    getch();
    exit(1);
}

char *toString(int num)
{
    char sign = 0;
    if (num<0) {
        sign = -1;
        num*=-1;
    }
    int numLen = 1;
    if (sign<0) {
        numLen++;
    }
    int tmpNum = num;
    while (tmpNum>9) {
        tmpNum /= 10;
        numLen++;
    }
    char *result = (char *)malloc(sizeof(char)*(numLen+1));
    result[numLen] = '\0';
    char ch;
    while (num>9) {
        ch = (num%10)+'0';
        num /= 10;
        result[numLen-1] = ch;
        numLen--;
    }
    result[numLen-1] = num + '0';

    if (sign<0)
        result[0] = '-';
    return result;
}

int toInteger(char *line)
{
    int i=strlen(line)-1;
    int factor = 1;
    int result = 0;
    while (i>=0) {
        result += factor*(line[i]-'0');
        factor *= 10;
        i--;
    }
    return result;
}

char *getNewParam(FILE *fp)
{
    char ch = 'X';
    char *newParam;
    newParam = (char*) malloc(1);
    newParam[0] = '\0';
    int paramSize = 0;
    while (!isspace(ch)) {
        ch = fgetc(fp);
        if (!isspace(ch)) {
            if (ch=='#') {
                while (fgetc(fp)!='\n');
                continue;
            }
            paramSize++;
            newParam = (char *) realloc(newParam, paramSize+1);
            newParam[paramSize-1] = ch;
        }
    }
    newParam[paramSize] = '\0';
    return newParam;
}

IMAGE readPGM()
{
    FILE *fp;
    IMAGE image;
    //Open the file.
    fp = fopen("seeds2.pgm","r+b");
    if (fp=='\0')
        raiseError("File could not be opened!..");

    //Check if it is a raw PGM(P5)
    char *line;
    line = getNewParam(fp);
    if (strcmp(line, "P5")!=0)
        raiseError("File is not a valid raw PGM(P5)");
    int paramCount = 0;
    int *pgmParams;
    pgmParams = (int *)malloc(sizeof(int)*3);
    while (paramCount<3) {
        line = getNewParam(fp);
        pgmParams[paramCount++] = toInteger(line);
    }
    int pixelSize;
    if (pgmParams[2]>255)
        pixelSize = 2;
    else
        pixelSize = 1;

    image.width =pgmParams[0];
    image.height =pgmParams[1];
    image.maxval =pgmParams[2];
    free(pgmParams);
    image.data = (char **) malloc(sizeof(char)*image.height);
    int i,j;
    long sum = 0;
    for (i=0; i<image.height; i++) {
        image.data[i] = (char *) malloc(sizeof(char)*image.width);
        sum += sizeof(char)*image.width;
        if (image.data[i]=='\0') {
            printf("%d\n", i);
            raiseError("Not enough memory!..");

        }
    }
    for (i=0; i<image.height; i++) {
        for (j=0; j<image.width; j++) {
            fread(&image.data[i][j], sizeof(char), image.width, fp);
        }
    }
    fclose(fp);

    return image;
}

void savePGM(IMAGE image)
{
    FILE *fp = fopen("yeni.pgm", "w+b");
    fprintf(fp, "P5\n%s\n%s\n%s\n",
        toString(image.width), toString(image.height), toString(image.maxval));
    int i,j;
    for (i=0; i<image.height; i++) {
        for (j=0; j<image.width; j++) {
            fwrite(&image.data[i][j], sizeof(char), 1, fp);
        }
    }
    fclose(fp);
}

int main()
{
    clrscr();
    IMAGE image = readPGM();
    //process
    savePGM(image);
    getch();
    return 0;
}

Antworten auf die Frage(3)

Ihre Antwort auf die Frage