相关疑难解决方法(0)

C内存分配:为什么没有足够的内存(仅限250K)

我无法弄清楚我的.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!..");
        }
    }

    //..
}

//..
Run Code Online (Sandbox Code Playgroud)

当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 …
Run Code Online (Sandbox Code Playgroud)

c memory malloc

2
推荐指数
2
解决办法
2921
查看次数

标签 统计

c ×1

malloc ×1

memory ×1