⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 malloc.c

📁 c21Examples.rar
💻 C
字号:
/* Using malloc() to determine free memory.*/

#include <stdio.h>
#include <stdlib.h>

/* Definition of a structure that is
   1024 bytes (1 kilobyte) in size.) */

struct kilo {
   struct kilo *next;
   char dummy[1022];
};

int FreeMem(void);

int main( void )
{

   printf("You have %d kilobytes free.\n", FreeMem());
   return 0;
}

int FreeMem(void)
{
   /*Returns the number of kilobytes (1024 bytes)
   of free memory. */

   long counter;
   struct kilo *head, *current, *nextone;

   current = head = (struct kilo*) malloc(sizeof(struct kilo));

   if (head == NULL)
      return 0;      /*No memory available.*/

   counter = 0;
   do
   {
      counter++;
      current->next = (struct kilo*) malloc(sizeof(struct kilo));
      current = current->next;
      printf("\r%d", counter);
   } while (current != NULL);

   /* Now counter holds the number of type kilo
      structures we were able to allocate. We
      must free them all before returning. */

   current = head;
   do
   {
      nextone = current->next;
      free(current);
      current = nextone;
   } while (nextone != NULL);

   return counter;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -