Why does calling sbrk(0) twice give a different value?

谁说胖子不能爱 提交于 2019-11-28 00:55:22

Your program performs the following sequence of calls:

sbrk()
printf()
sbrk()
printf()
...

The first call to printf calls malloc internally to allocate a buffer for stdout (stdout is line buffered by default, but the buffer is created on demand the first time you print to it).

That's why the second call to sbrk returns a different value.

(This answer is not directly related, but the error messages from valgrind expose the existence of the underlying malloc call hidden inside printf.)

Your second example performs all sbrk calls up front, so there are no surprises from other functions calling malloc behind your back.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!