2011年2月7日 星期一

[C]malloc 與 free 使用

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

/*沒配置到記憶體結束程式*/
if (ptr_i == NULL) {
  printf("function malloc can't alllocate memory!\n");
  system("PAUSE");
  return 0;
}

*ptr_i = 5; /*設定i值

free(ptr_i); /*釋放 ptr_i 所指向的空間 */
ptr_i = NULL; /*制定 ptr_i 為 NULL */

system("PAUSE");
return 0;

沒有留言:

張貼留言