为什么MALLOC在C语言中的功能比机器配置分配更多的内存?

发布于 2025-01-23 22:26:15 字数 3318 浏览 3 评论 0原文

我正在使用C中的Malloc功能分配内存,但是我遇到了一个奇怪的问题,我对此感到困惑。

以下是程序代码:

#include <stdio.h>
#include <stdlib.h>

#define _1M (1024 * 1024)

// TODO stackOverFlow question
int main(int c, char *v[]) {

    size_t max_allocatable_mem;
    long i = 1;
    char *addr = NULL;

    while (1) {

        max_allocatable_mem = _1M * i;
        addr = malloc(max_allocatable_mem);

        if (addr) {
            printf("allocatable % 16ld M\n", i++);
            free(addr);
        } else {
            printf("can not be allocated % 16ld M\n", i--);
            break;
        }

    }

    printf("\nmax_allocatable_mem = %ld M\n\n", i);

    size_t small_mem_block = max_allocatable_mem / 10;

    for (i=0; i<20; i++) {
        addr = malloc(small_mem_block);
        if (addr) {
            printf("starting address < %p >  capacity < %lu bytes >\n", addr, small_mem_block);
        } else {
            break;
        }
    }

    return 0;
}

该程序最初使用malloc函数分配最大内存,然后记录可以分配的最大内存。然后,进行小记忆的分配。每次分配的小内存是最大可分配内存的十分之一。它连续分配20次,每次Malloc都可以返回特定的内存地址。通过这种方式,分配的小内存稍后总和是该程序首先可以分配的最大内存的两倍。

这是在我的计算机上执行此代码结果的结果的输出:

allocatable             1332 M
allocatable             1333 M
allocatable             1334 M
allocatable             1335 M
allocatable             1336 M
can not be allocated             1337 M

max_allocatable_mem = 1336 M

starting address < 0x7f8787a4c010 >  capacity < 140194611 bytes >
starting address < 0x7f877f498010 >  capacity < 140194611 bytes >
starting address < 0x7f8776ee4010 >  capacity < 140194611 bytes >
starting address < 0x7f876e930010 >  capacity < 140194611 bytes >
starting address < 0x7f876637c010 >  capacity < 140194611 bytes >
starting address < 0x7f875ddc8010 >  capacity < 140194611 bytes >
starting address < 0x7f8755814010 >  capacity < 140194611 bytes >
starting address < 0x7f874d260010 >  capacity < 140194611 bytes >
starting address < 0x7f8744cac010 >  capacity < 140194611 bytes >
starting address < 0x7f873c6f8010 >  capacity < 140194611 bytes >
starting address < 0x7f8734144010 >  capacity < 140194611 bytes >
starting address < 0x7f872bb90010 >  capacity < 140194611 bytes >
starting address < 0x7f87235dc010 >  capacity < 140194611 bytes >
starting address < 0x7f871b028010 >  capacity < 140194611 bytes >
starting address < 0x7f8712a74010 >  capacity < 140194611 bytes >
starting address < 0x7f870a4c0010 >  capacity < 140194611 bytes >
starting address < 0x7f8701f0c010 >  capacity < 140194611 bytes >
starting address < 0x7f86f9958010 >  capacity < 140194611 bytes >
starting address < 0x7f86f13a4010 >  capacity < 140194611 bytes >
starting address < 0x7f86e8df0010 >  capacity < 140194611 bytes >

为什么能够连续分配多个小内存的能力加起来多个分配了最大的内存? 可能是掉期的影响吗?但是交换在我的计算机上被关闭。

[root@ps-5 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1881820      434676      462456         632      984688     1288636
Swap:             0           0           0

我希望得到帮助。

谢谢大家。

I'm allocating memory using malloc function in C, but I'm having a weird problem and I'm confused about it.

Below is the program code:

#include <stdio.h>
#include <stdlib.h>

#define _1M (1024 * 1024)

// TODO stackOverFlow question
int main(int c, char *v[]) {

    size_t max_allocatable_mem;
    long i = 1;
    char *addr = NULL;

    while (1) {

        max_allocatable_mem = _1M * i;
        addr = malloc(max_allocatable_mem);

        if (addr) {
            printf("allocatable % 16ld M\n", i++);
            free(addr);
        } else {
            printf("can not be allocated % 16ld M\n", i--);
            break;
        }

    }

    printf("\nmax_allocatable_mem = %ld M\n\n", i);

    size_t small_mem_block = max_allocatable_mem / 10;

    for (i=0; i<20; i++) {
        addr = malloc(small_mem_block);
        if (addr) {
            printf("starting address < %p >  capacity < %lu bytes >\n", addr, small_mem_block);
        } else {
            break;
        }
    }

    return 0;
}

The program initially uses the malloc function to allocate the maximum memory, and then records the maximum memory that can be allocated. Then, the allocation of small memory is carried out. The small memory allocated each time is one-tenth of the maximum allocatable memory. It is allocated 20 times in a row, and each time malloc can return a specific memory address. In this way, the small memory allocated later adds up to twice the maximum memory the program can allocate in the first place.

Here is the output of the result of executing this code on my computer:

allocatable             1332 M
allocatable             1333 M
allocatable             1334 M
allocatable             1335 M
allocatable             1336 M
can not be allocated             1337 M

max_allocatable_mem = 1336 M

starting address < 0x7f8787a4c010 >  capacity < 140194611 bytes >
starting address < 0x7f877f498010 >  capacity < 140194611 bytes >
starting address < 0x7f8776ee4010 >  capacity < 140194611 bytes >
starting address < 0x7f876e930010 >  capacity < 140194611 bytes >
starting address < 0x7f876637c010 >  capacity < 140194611 bytes >
starting address < 0x7f875ddc8010 >  capacity < 140194611 bytes >
starting address < 0x7f8755814010 >  capacity < 140194611 bytes >
starting address < 0x7f874d260010 >  capacity < 140194611 bytes >
starting address < 0x7f8744cac010 >  capacity < 140194611 bytes >
starting address < 0x7f873c6f8010 >  capacity < 140194611 bytes >
starting address < 0x7f8734144010 >  capacity < 140194611 bytes >
starting address < 0x7f872bb90010 >  capacity < 140194611 bytes >
starting address < 0x7f87235dc010 >  capacity < 140194611 bytes >
starting address < 0x7f871b028010 >  capacity < 140194611 bytes >
starting address < 0x7f8712a74010 >  capacity < 140194611 bytes >
starting address < 0x7f870a4c0010 >  capacity < 140194611 bytes >
starting address < 0x7f8701f0c010 >  capacity < 140194611 bytes >
starting address < 0x7f86f9958010 >  capacity < 140194611 bytes >
starting address < 0x7f86f13a4010 >  capacity < 140194611 bytes >
starting address < 0x7f86e8df0010 >  capacity < 140194611 bytes >

Why can the capacity of successively allocating multiple small memory add up to more than one allocating the largest memory?
Could it be the influence of Swap? But Swap is turned off on my computer.

[root@ps-5 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1881820      434676      462456         632      984688     1288636
Swap:             0           0           0

I hope to get help.

Thank you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文