// the malloc style, which returns a pointer:
struct Cat *newCat = malloc(sizeof(struct Cat));
// no malloc...but isn't it actually the same thing? uses memory as well, or not?
struct Cat cat = {520.0f, 680.0f, NULL};
Run Code Online (Sandbox Code Playgroud)
基本上,我可以通过这两种方式获得初始化结构.我的猜测是:它是一样的,但是当我使用malloc时我也必须释放().在第二种情况下,我不必考虑内存,因为我不调用malloc.也许.
我何时应该使用malloc风格,何时使用另一个?
它不是相同的内存,前者在堆上分配,后者在堆栈上分配.
堆使您可以手动控制创建和删除,所有优点和缺点:您可以将指针传递给其他人,但您也必须删除它.
只要第二个是可能的,它就是首选.