在 C 的 DOSBOX 中没有可用于 malloc 的内存

No memory available for malloc in DOSBOX with C

我正在开发一个程序,用 C 使用 BORLAND 3.1 在 DOSBOX 上创建一个基本的图像编辑器(如画图)。现在我正在尝试实现一个简单的撤消按钮。为此,我创建了一个双数组来存储 10 倍的绘图区域(即 288*180)。但是,当添加数组初始化行时,我无法为我在程序的其他功能中使用的双缓冲区分配内存。

有什么方法可以让我在 DOSBOX 或其他不会出现此问题的实现中获得更多内存?

我这样编译我的程序:

bcc -mh paint.c

这是我的代码:

byte huge undo[51840][10];  // This is the array that is giving me problems
void welcome_screen(){
    BITMAP fondo_inicio,normal_ptr_image,boton_inicio,boton_salir;
    MOUSE  mouse_welcome;
    unsigned long int  h;
    int a[2];
    byte *double_buffer;
    sword new_x, new_y;
    word redraw,press,release;
    sword dx,dy=0;
    MOUSEBITMAP *normal_pointer=NULL;
    MOUSEBITMAP *mouse_new=NULL;
    word last_time;

    int i,done = 0;

    filled=-1;

    /* allocate memory for double buffer and background image SCREEN_SIZE = 320*200 */
    if ((double_buffer = (byte *) malloc(SCREEN_SIZE)) == NULL)
    {
        printf("Not enough memory for double buffer.\n"); // ---> This is the error I get when adding the above line
        exit(1);
    }

与其将撤消缓冲区保留为 10 个全屏位图的数组,不如保留 一个 屏幕和随后的 10 个后续操作?然后撤消只是恢复屏幕缓冲区,然后应用随后记住的 9 个操作。

也就是...

byte undo[288*180]; // one screen of memory representing what the screen looked like 10 operations ago

struct Operation undo_ops[10]; // the last 10 operations since the undo buffer was formed
int undo_ops_count; // number of elements in above array

其中 "Operation" 是这样的:

struct Operation
{
    int x1;
    int y1;
    int x2;
    int y2;
    int size;
    int extra;
    int color;
    enum OpCode code; // Circle, Rectangle, Line, Fill, whatever...
};

然后是 "Undo" 实际执行的一些伪代码

void Undo()
{
    if (undo_ops_count > 0)
    {
        undo_ops_count--;  // forget the last operation

        // restore the screen
        memcpy(g_screen_buffer, undo, sizeof(undo));

        // apply the last operations before the last user operation
        for (int x = 0; x < undo_ops_count; x++)
        {
            ApplyOperation(g_screen_buffer, &undo_ops[x]); // re-draw what the user did on top of the restored screen buffer
        }
        RepaintScreen(g_screen_buffer);
    }
}

绘制新内容的每个用户操作都会附加到 undo_ops 数组。如果您 运行 超出数组 space 的 undo_ops,您只是重新绘制一个新的屏幕缓冲区并删除最旧的操作。我会把它留给你作为练习。看看这如何释放更多内存,我敢打赌您可以记住 10 多个操作而不会 运行ning 内存不足。

如果你想使用640K以上的内存,你要做的就是用DPMI编译,用CWSDMPI这样的DOS扩展器,通过它分配内存。 DJGPP 是 GCC 的 DOS 端口,是可用于 DOS 的最现代的编译器,并且仍在开发中。 (编辑: 根据 Ross Ridge 的说法,DJGPP 通过标准库调用自动使用扩展内存。)当时大多数商业软件使用的另一个选项是 Watcom C DOS4G/W。今天可作为 OpenWatcom 和 DOS/32 扩展器使用。您还需要配置 DOSBOX 以使 XMS 可用; 16MiB 在 1995 年是一个很大的数目。