malloc 函数
1 | void *malloc(int size); |
说明:
malloc 向系统申请分配指定size 个字节的内存空间。返回类型是void* 类型。void* 表示未确定类型的指针。C,C++规定,void* 类型可以强制转换为任何其它类型的指针。
malloc() 工作机制
malloc() 在操作系统中的实现
在 C 程序中,多次使用
在大部分操作系统中,内存分配由以下两个简单的函数来处理:
1 2 | void *malloc (long numbytes):该函数负责分配 numbytes 大小的内存,并返回指向第一个字节的指针。 void free(void *firstbyte):如果给定一个由先前的 malloc 返回的指针,那么该函数会将分配的空间归还给进程的“空闲空间”。 |
1 2 3 4 5 | //清单 1. 我们的简单分配程序的全局变量 int has_initialized = 0; void *managed_memory_start; void *last_valid_address; |
如前所述,被映射的内存的边界(最后一个有效地址)常被称为系统中断点或者当前中断点。在很多
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 清单 2. 分配程序初始化函数 /* Include the sbrk function */ #include void malloc_init() { /* grab the last valid address from the OS */ last_valid_address = sbrk(0); /* we don''t have any memory to manage yet, so *just set the beginning to be last_valid_address */ managed_memory_start = last_valid_address; /* Okay, we''re initialized and ready to go */ has_initialized = 1; } |
现在,为了完全地管理内存,我们需要能够追踪要分配和回收哪些内存。在对内存块进行了
1 2 3 4 5 | //清单 3. 内存控制块结构定义 struct mem_control_block { int is_available; int size; }; |
现在,您可能会认为当程序调用
在讨论分配内存之前,我们将先讨论释放,因为它更简单。为了释放内存,我们必须要做的惟一一件事情就是,获得我们给出的指针,回退
1 2 3 4 5 6 7 8 9 10 11 12 | //清单4. 解除分配函数 void free(void *firstbyte) { struct mem_control_block *mcb; /* Backup from the given pointer to find the * mem_control_block */ mcb = firstbyte - sizeof(struct mem_control_block); /* Mark the block as being available */ mcb->is_available = 1; /* That''s It! We''re done. */ return; } |
如您所见,在这个分配程序中,内存的释放使用了一个非常简单的机制,在固定时间内完成内存释放。分配内存稍微困难一些。我们主要使用连接的指针遍历内存来寻找开放的内存块。这里是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 1 //清单 6. 主分配程序 2 void *malloc(long numbytes) { 3 /* Holds where we are looking in memory */ 4 void *current_location; 5 /* This is the same as current_location, but cast to a 6 * memory_control_block 7 */ 8 struct mem_control_block *current_location_mcb; 9 /* This is the memory location we will return. It will 10 * be set to 0 until we find something suitable 11 */ 12 void *memory_location; 13 /* Initialize if we haven''t already done so */ 14 if(! has_initialized) { 15 malloc_init(); 16 } 17 /* The memory we search for has to include the memory 18 * control block, but the users of malloc don''t need 19 * to know this, so we''ll just add it in for them. 20 */ 21 numbytes = numbytes + sizeof(struct mem_control_block); 22 /* Set memory_location to 0 until we find a suitable 23 * location 24 */ 25 memory_location = 0; 26 /* Begin searching at the start of managed memory */ 27 current_location = managed_memory_start; 28 /* Keep going until we have searched all allocated space */ 29 while(current_location != last_valid_address) 30 { 31 /* current_location and current_location_mcb point 32 * to the same address. However, current_location_mcb 33 * is of the correct type, so we can use it as a struct. 34 * current_location is a void pointer so we can use it 35 * to calculate addresses. 36 */ 37 current_location_mcb = 38 (struct mem_control_block *)current_location; 39 if(current_location_mcb->is_available) 40 { 41 if(current_location_mcb->size >= numbytes) 42 { 43 /* Woohoo! We''ve found an open, 44 * appropriately-size location. 45 */ 46 /* It is no longer available */ 47 current_location_mcb->is_available = 0; 48 /* We own it */ 49 memory_location = current_location; 50 /* Leave the loop */ 51 break; 52 } 53 } 54 /* If we made it here, it''s because the Current memory 55 * block not suitable; move to the next one 56 */ 57 current_location = current_location + 58 current_location_mcb->size; 59 } 60 /* If we still don''t have a valid location, we''ll 61 * have to ask the operating system for more memory 62 */ 63 if(! memory_location) 64 { 65 /* Move the program break numbytes further */ 66 sbrk(numbytes); 67 /* The new memory will be where the last valid 68 * address left off 69 */ 70 memory_location = last_valid_address; 71 /* We''ll move the last valid address forward 72 * numbytes 73 */ 74 last_valid_address = last_valid_address + numbytes; 75 /* We need to initialize the mem_control_block */ 76 current_location_mcb = memory_location; 77 current_location_mcb->is_available = 0; 78 current_location_mcb->size = numbytes; 79 } 80 /* Now, no matter what (well, except for error conditions), 81 * memory_location has the address of the memory, including 82 * the mem_control_block 83 */ 84 /* Move the pointer past the mem_control_block */ 85 memory_location = memory_location + sizeof(struct mem_control_block); 86 /* Return the pointer */ 87 return memory_location; 88 } |
这就是我们的内存管理器。现在,我们只需要构建它,并在程序中使用它即可.多次调用
malloc/new函数具体区别
属性
参数
使用