简体   繁体   中英

calloc and non contiguous memory blocks and void pointer

calloc 函数用于保留内存并给出内存块的起始地址,但据说它可能不会在连续的地址空间中分配,而是我分配了不同的不同的非连续块,但我们作为 calloc 的返回值获得的起始地址只会指向从起始地址开始的连续内存,那么如果内存分配了两个内存块,那么当一个连续块被占用时,指针将如何跳转到另一个内存块

void * calloc (size_t n, size_t size)

but it is said that it may not allocate in contiguous address space

Where is this "said" and what's the context?

calloc() returns a pointer to a contiguous block (in virtual address space) of size nelem * size .

Consecutive calls to calloc() don't give you adjacent blocks (in general).

Where did you read that:

it my allocate different different non contiguous blocks but starting address > we get as return value of calloc will only point to contiguous memory from starting address

The C library function void *calloc(size_t nitems, size_t size) allocates the requested memory and returns a pointer to it. The difference in malloc and calloc is that malloc does not set the memory to zero where as calloc sets allocated memory to zero.

http://en.cppreference.com/w/c/memory/calloc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM