相关疑难解决方法(0)

由C11 aligned_alloc分配的内存重新分配是否保持对齐?

考虑以下(C11)代码:

void *ptr = aligned_alloc(4096, 4096);
... // do something with 'ptr'
ptr = realloc(ptr, 6000);
Run Code Online (Sandbox Code Playgroud)

由于ptr指向的内存具有4096字节的对齐aligned_alloc,是否(读取:是否保证)在(成功)调用之后保持该对齐realloc?或者内存可以恢复为默认对齐?

c memory-management realloc c11

10
推荐指数
1
解决办法
1283
查看次数

是否有相当于 _aligned_realloc 的 linux

_aligned_realloc的 linux 等价物吗?

我想使用 realloc 所以我不必每次调整数据时都对数据进行 memcpy。我被 mmap 卡住了吗?我只使用了一次 mmap 是否有推荐的方法来实现将调整几次大小的内存?我假设我不能将 mmap 与aligned_alloc 混合使用,并且我必须在第一次调整大小时执行memcpy?(或始终使用 mmap)

下面的 realloc 并不总是对齐。我在(64位)linux下使用gcc和clang进行了测试

#include<cstdlib>
#include<cstdio>
#define ALIGNSIZE 64
int main()
{
    for(int i = 0; i<10; i++)
    {
        void *p = aligned_alloc(ALIGNSIZE, 4096);
        void *p2 = realloc(p, 4096+2048); //This doesn't always align
        //void *p3 = aligned_alloc(ALIGNSIZE, 4096/24); //Doesn't need this line to make it unaligned. 

        if (((long)p & (ALIGNSIZE-1)) != 0 || ((long)p2 & (ALIGNSIZE-1)) != 0)
            printf("%d %d %d\n", i, ((long)p & …
Run Code Online (Sandbox Code Playgroud)

c c++ linux

4
推荐指数
1
解决办法
217
查看次数

标签 统计

c ×2

c++ ×1

c11 ×1

linux ×1

memory-management ×1

realloc ×1