C++ new Test[n] 完整内存分配流程

C++ new Test[n] C++ operator new[](n * sizeof(T) + 8) malloc(size) __libc_malloc(size) nb = checked_request2size(size) nb < tcache_max_bytes ? (约1MB) yes tcache->entries[idx] != NULL ? yes tcache_get() no no __libc_malloc2(size) arena_get() (multithreaded) _int_malloc(arena, nb) fastbin (≤128B) smallbin (<1024B) malloc_consolidate() (nb≥1024时) 合并fastbin→unsorted unsorted bin (traverse & sort) large bin (≥1024B, best-fit) top chunk (cut) sysmalloc() sbrk() / mmap() ↑ 串行尝试: fastbin → smallbin → unsorted → large → top → sysmalloc set_head(victim, nb | P) return chunk2mem(victim) // chunk + 16 Write cookie (if needed) 调用 n 次构造函数 T() return mem + 8 // 数组首地址 最终内存布局 (new Test[5], sizeof(Test)=4) prev_size (8B) size | P (8B) = 0x31 cookie (8B) = 5 arr[0]...arr[4] (20B 用户数据) pad (4B) ↑ chunk ↑ mem (malloc返回) ↑ arr (new[]返回) 总共 48 bytes (0x30), size字段 = 0x31 (48 | PREV_INUSE)