VC++的malloc实现代码

这篇博客详细介绍了VC++中C运行时库(CRT)的malloc.c文件,该文件包含了malloc()函数的实现。文章探讨了如何从堆中获取内存块,并涉及到了内存分配的细节,如大小验证、内存块的分裂和新的处理程序机制。源代码分析涵盖了不同内存分配情况的处理,包括WinHeap和非WinHeap的情况。
摘要由CSDN通过智能技术生成

导读:



malloc.c属于C的运行时函数库(C Runtime Library)文件

VC安装时默认没有把代码复制到硬盘,可以通过添加删除组件把文件复制到硬盘上

VC++ Runtime Libraries / CRT Source Code

这样在C:/Program Files/Microsoft Visual Studio/VC98下会出现一个CRT的文件夹

malloc.c文件的地址是:

C:/Program Files/Microsoft Visual Studio/VC98/CRT/SRC/malloc.c

 

/***
*malloc.c - Get a block of memory from the heap
*
* Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* Defines the malloc() function.
*
*******************************************************************************/

#include
#include
#include
#include
#include

#ifdef WINHEAP
#include
#include
#else /* WINHEAP */
#include
#endif /* WINHEAP */


extern int _newmode; /* malloc new() handler mode */


/***
*void *malloc(size_t size) - Get a block of memory from the heap
*
*Purpose:
* Allocate of block of memory of at least size bytes from the heap and
* return a pointer to it.
*
* Calls the new appropriate new handler (if installed).
*
*Entry:
* size_t size - size of block requested
*
*Exit:
* Success: Pointer to memory block
* Failure: NULL (or some error value)
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/

void * __cdecl _malloc_base (size_t size)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嵌入式系统中的动态内存分配通常需要使用 malloc 函数进行实现。下面是一个简单的嵌入式系统中 malloc 函数的实现代码示例: ```c // 定义内存池大小 #define MEMORY_POOL_SIZE 1024 // 内存池 static unsigned char memory_pool[MEMORY_POOL_SIZE]; // 内存块结构体 typedef struct { size_t size; // 内存块大小 int used; // 是否已被使用 } MemoryBlock; // 内存块列表 static MemoryBlock memory_blocks[MEMORY_POOL_SIZE]; // 初始化内存池 void init_memory_pool() { for (int i = 0; i < MEMORY_POOL_SIZE; i++) { memory_blocks[i].size = 0; memory_blocks[i].used = 0; } } // malloc 函数实现 void* my_malloc(size_t size) { for (int i = 0; i < MEMORY_POOL_SIZE; i++) { if (memory_blocks[i].used == 0 && memory_blocks[i].size >= size) { memory_blocks[i].size = size; memory_blocks[i].used = 1; return &memory_pool[i]; } } return NULL; } // free 函数实现 void my_free(void* ptr) { if (ptr == NULL) return; unsigned char* p = (unsigned char*)ptr; int index = p - memory_pool; if (index >= 0 && index < MEMORY_POOL_SIZE) { memory_blocks[index].size = 0; memory_blocks[index].used = 0; } } ``` 这段代码中,我们使用一个静态数组 `memory_pool` 作为内存池,通过 `memory_blocks` 数组来记录每个内存块的大小和使用情况。在 `my_malloc` 函数中,我们遍历内存块列表,找到第一个大小足够且未被使用的内存块,将其标记为已使用,并返回对应的指针。在 `my_free` 函数中,我们通过计算指针在内存池中的偏移量,找到对应的内存块,并将其标记为未使用。 请注意,这只是一个简单的示例实现,仅用于理解嵌入式系统中 malloc 的基本原理。在实际应用中,还需要考虑内存对齐、内存碎片等问题,并根据具体的嵌入式系统做出适当的改进和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值