C - Pointer memory allocation [duplicate]

前提是你 提交于 2019-12-11 12:05:19

问题


Can someone please explain to me the difference between

int *x = malloc(sizeof(int));

&&

int *x = (int*)malloc(sizeof(int));

Thanks!


回答1:


The difference is that you are casting the return of malloc() in the second example. malloc() returns a void* pointer, which is automatically and safely promoted to any other pointer type in this case.

Therefore casting in this case is not required and should not be done. Check here.



来源:https://stackoverflow.com/questions/33798608/c-pointer-memory-allocation

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