malloc的返回负值 [英] malloc returns negative value

查看:158
本文介绍了malloc的返回负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行在硬件这块code的。

I am running this piece of code on a hardware.

unsigned char *buf;

buf = malloc(sizeof(int));
printf("Address of buf %d\n" , &buf);

if(!buf)
    return MEMORYALLOC_FAILURE;

的malloc 正在返回负值。可能是什么问题?

The malloc is returning negative value. What could be the problem?

推荐答案

malloc返回地址没有正面或负面的,它只是解决,使用%p将其打印出来,而不是%d:

Address returned by malloc is not negative or positive, it just address, use %p to print it, not %d:

printf("Address of buf %p\n" , &buf);

如果你要打印从malloc返回的地址,去掉符号:

And if you want to print the address returned from malloc, remove the ampersand:

printf("Address of buf %p\n" , buf);

这篇关于malloc的返回负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆