关于动态内存分配的C编程面试问题

huangapple go评论91阅读模式
英文:

C programming interview question about dynamic memory allocation

问题

如果要在堆中动态分配8个字节的内存,但这8个字节的内存不连续可用。它以5字节和3字节分开在堆中可用。那么你能分配这块内存吗?如果可以,如何操作?

如果可以,我们可以只使用malloc还是malloc和calloc一起使用?

英文:

If you want to allocated 8 bytes of memory dynamically in the heap but this 8 bytes of memory is not available continuously. It is available as 5 bytes and 3 bytes separately in the heap. Then can you allocate the memory? if yes, how?

If yes, Can we use only malloc or malloc with calloc

答案1

得分: 6

堆的粒度通常为8或16字节。很不可能有2个块分别可用于5字节和3字节,并且它们都不可用于分配8字节的情况。如果内存非常稀缺,您遇到了这种情况,尝试使用malloc()calloc()分配8字节块失败,您不应该尝试使用2个单独的块来处理需求,而应该报告内存不足情况并优雅地处理它。

英文:

The granularity of the heap is typically 8 or 16 bytes. It is rather unlikely that there be 2 blocks available for respectively 5 and 3 bytes and neither of them be available for an allocation of 8 bytes. If memory is so scarce you encounter this situation and allocating an 8 byte block with malloc() or calloc() fails, you should not try to handle the requirement with 2 separate blocks, but rather signal an out of memory condition and handle it gracefully.

答案2

得分: 1

If you want to allocated 8 bytes of memory dynamically in the heap but this 8 bytes of memory is not available continuously.
It is available as 5 bytes and 3 bytes separately in the heap.
Then can you allocate the memory?

每次成功调用 malloc()(或 calloc())都会分配一个连续的内存块。如果没有足够大的连续块可用,那么malloc()calloc())将失败。malloc()没有任何方法返回不连续的内存块。而且它不能移动已分配的块,因此无法合并不已连续的块。

那么,你能分配一个8字节的块吗?不,你不能。

你能分配两个块,总大小为8字节吗?如果我们理解这个问题是在断言一个5字节块和一个独立的3字节块可以分别分配,那么答案是肯定的:这是问题中规定的。如果实际问题是这样的,那么潜在问题可能是如何发现这个5字节和3字节块是否可用。没有内存检查机制可以让你查找可用块的组合,但你可以尝试越来越小的大小,直到找到适合的块,然后看看是否可以分配剩余的部分。例如:

void allocate8(void **ret1, void **ret2) {
    void *p1 = NULL;
    void *p2 = NULL;

    for (int i = 8; i >= 4; i--) {
        p1 = malloc(i);
        if (p1) {
            if (i < 8) {
                p2 = malloc(8 - i);
                if (!p2) {
                    free(p1);
                    p1 = NULL;
                }
            }
            break;
        }
    }

    *ret1 = p1;
    *ret2 = p2;
}

但这是否有用呢?几乎肯定不会。

英文:

> If you want to allocated 8 bytes of memory dynamically in the heap but this 8 bytes of memory is not available continuously.
It is available as 5 bytes and 3 bytes separately in the heap.
Then can you allocate the memory?

Each successful call to malloc() (or calloc()) allocates a contiguous block of memory. If there is no large enough block available then malloc() (calloc()) will fail. malloc() does not have any way to return discontinuous memory blocks. And it must not move allocated blocks, so it cannot coalesce blocks that are not already contiguous.

So, can you allocate an 8-byte chunk? No, you cannot.

Can you allocate two chunks, with a combined size of 8 bytes? If we understand the question as asserting that a 5-byte chunk and a separate 3-byte chunk can be allocated separately, then yes: that is stipulated by the question. If that's what is actually being asked, then the underlying issue is probably how you can discover that the 5-byte and 3-byte chunks are available. There is no memory-introspection mechanism by which you could look for a combination of available blocks, but you could simply try smaller and smaller sizes until you find one that works, and then see whether you can allocate the remainder. For example:

void allocate8(void **ret1, void **ret2) {
    void *p1 = NULL;
    void *p2 = NULL;

    for (int i = 8; i &gt;= 4; i--) {
        p1 = malloc(i);
        if (p1) {
            if (i &lt; 8) {
                p2 = malloc(8 - i);
                if (!p2) {
                    free(p1);
                    p1 = NULL;
                }
            }
            break;
        }
    }

    *ret1 = p1;
    *ret2 = p2;
}

But is that useful? Almost surely not.

答案3

得分: 1

如果我理解正确你所描述的问题,它涉及到内存碎片化。不幸的是,作为开发者,你无法特别修复这个问题,尤其是只能使用malloc()和calloc()函数的情况下,你能做的最好的事情就是意识到这个问题并尝试捕捉它。解决这个问题的方法取决于系统中使用的内存分配器。

英文:

If I understood correctly the problem you described, it concerns memory fragmentation. Unfortunately as a developer you can't remedy this especially being limited to only malloc() and calloc() functions, the best you can do is be aware of the problem and try to catch it. The solution to the problem depends on the memory allocator used in the system.

huangapple
  • 本文由 发表于 2023年8月5日 03:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定