C 内存分配 : Why there is not enough memory(250K only)

标签 c memory malloc

我无法弄清楚我的 .c 代码在分配 ~250K 内存时遇到问题的原因。这是分配代码:

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!..");
        }
    }

    //..
}

//..

程序在 i=116 时退出。 image.width 和 image.height 在这里等于 500,所以我想在这里分配 500x500=250000 字节。但是最多分配 116x500 = 58000 字节。那么,有什么限制它的吗?我的代码有问题吗?我在下面发布了完整的源代码,以防万一。这个想法是将一个 PGM 文件读入结构 IMAGE,处理它并在另一个文件中重写它。如您所知,它还没有完成,因为我想不出分配更多内存的方法。

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

最佳答案

您问题的答案在您添加的评论中。您正在使用(古董)16 位 x86 实模式编译器。一个 16 位虚拟机总共只能寻址 1Mb 的内存,其中只有 640Kb 可供程序正常访问,并与操作系统共享。

[根据 paxdiablo 的评论进行编辑]

除了这些限制之外,分段寻址架构还产生了许多内存模型,其中低至 64kb 可能是特定内存区域的限制。

此外,malloc() 的参数具有 size_t 类型,在这种情况下可能只是 16 位 - 您应该检查一下。我记得使用一个名为 halloc() 的变体来进行大量分配。但是如果不使用 DOS 扩展程序,仍然有 640kb 的限制。

[结束编辑]

有许多更好的免费现代 32 位编译器可用。丢掉古董。我建议 VC++ Express Edition

除此之外:

if(image.data[i]=='\0')

应该是

if(image.data[i]==0)

if(image.data[i]==NULL)

碰巧它在任何一种情况下都可以工作,但从技术上讲,您测试的是空指针而不是 NUL 字符。

关于C 内存分配 : Why there is not enough memory(250K only),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1658579/

相关文章:

c - 在程序的生命周期中使用 malloc

c++ - 此代码似乎在分配范围之外附加字符

c - 编写一个程序来计算一个字符在文件中出现的次数。 (不区分大小写... 'a'和 'A'被认为是相同的)

java字节数组内存未释放

memory - 汇编器: How are segments used in 32bit systems?

c - 内存地址如何放置在二进制文件中?

c - 在特殊情况下使用 void** 是否安全?还是未定义的行为?

c - 从 C 中的偶数地址中删除字节

c - linux中c "fopen"可以打开的最大文件数

c - 当子进程通过 ctrl-\中止时,父进程未运行