类型转换在动态内存分配中扮演什么角色?

What role does type casting play in dynamic memory allocation?

据我所知,可以使用 malloc 函数在 C 中完成动态内存分配:

     int *p = (int *)malloc(sizeof(int))

我的问题是:

  1. 动态内存分配是如何工作的,它与正常方法(即静态分配)在工作方面有何不同?

  2. malloc()函数前的语句(int *)有什么作用?

  3. 类型转换在这里扮演什么角色? (如果是这样的话)

How does dynamic memory allocation work and how is it different from the normal method in terms of working ?

您有库管理在 运行 时间从操作系统分配的内存页面。该库管理这些页面并将它们细分以处理 malloc() 调用发出的通常较小的请求。

相比之下,静态变量是由链接器和加载器分配的。链接器定义程序在内存中的布局,并创建一个可执行文件,其中包含用于加载器设置程序初始状态的指令。

what is the role of the statement (int *) before the malloc() function ?

消除您可能从隐式 (void*) 到 (int*) 转换中收到的警告消息

What role does type casting play here ? (if it does that is )

见上文。

默认malloc() returns 空指针。