[英]malloc not fail in Keil C
How can i check whether malloc() was fail in Keil C? 如何在Keil C中检查malloc()是否失败?
unsigned char xdata malloc_mempool [0x100];
void display()
{
unsigned char xdata *ptr;
int a;
init_mempool (&malloc_mempool, sizeof(malloc_mempool));
ptr = malloc(9000000);
if(ptr != 0)
{
a = 7;
free(ptr);
} else {
a = 9;
}
}
As stated in http://www.keil.com/support/man/docs/c51/c51_malloc.htm malloc will return null pointer if there is not enough memory to satisfy the allocation request. 如http://www.keil.com/support/man/docs/c51/c51_malloc.htm中所述,如果没有足够的内存来满足分配请求,malloc将返回空指针。 It is obviously not so mush memory in 8051. But the result of a is always 7. 在8051中,它显然不是那么简单的记忆。但是a的结果总是7。
From Understanding The Memory Organization Of 8051 Microcontroller the 8051 has a maximum 64KB of memory. 从了解8051微控制器的存储器组织来看,8051具有最大64KB的存储器。
So it will be using 2 bytes for pointers. 所以它将使用2个字节作为指针。 9000000 decimal is 0x895440 in hex, which is 3 bytes. 9000000十进制是十六进制的0x895440,即3个字节。
Very probably the malloc() function will just ignore the bits it cannot use, so in reality the call you are making is 很可能malloc()函数会忽略它不能使用的位,所以实际上你正在进行的调用是
ptr = malloc(0x5440);
which is 是的
ptr = malloc(21568);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.