[C] 使用malloc函数从堆上分配内存 →→→→→进入此内容的聊天室

来自 , 2019-04-20, 写在 C, 查看 104 次.
URL http://www.code666.cn/view/81b073de
  1. /* 使用malloc函数从堆上分配内存 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int main(void) {
  7.         int * p = NULL;
  8.         /*
  9.          *p = 2;                                此时对*p赋值为错误的。
  10.          */
  11.         p = (int *) malloc(sizeof(int));
  12.         if (NULL == p) {
  13.                 printf("Can’t get memory!\n");
  14.                 return -1;
  15.         }
  16.  
  17.         printf("%d\n", *p);
  18.         memset(p, 0, sizeof(int));
  19.         printf("%d\n", *p);
  20.         *p = 2;
  21.         printf("%d\n", *p);
  22.  
  23.         return 0;
  24. }
  25.  

回复 "使用malloc函数从堆上分配内存"

这儿你可以回复上面这条便签

captcha