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

📄 chap15.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
字号:
listing 2
#include <malloc.h> 
#include <stdio.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  char *str; 
 
  if(!(str = (char *) alloca(80))) { 
    printf("Allocation error."); 
    exit(1); 
  } 
  /* ... */ 
  return 0; 
}

listing 4
#include <stdlib.h> 
#include <stdio.h> 
 
float *get_mem(void) 
{ 
  float *p; 
 
  p = (float *) calloc(100, sizeof(float)); 
  if(!p) { 
    printf("Allocation failure."); 
    exit(1); 
  } 
  return p; 
}

listing 11
#include <stdlib.h> 
#include <stdio.h> 
 
int main(void) 
{ 
  char *str[100]; 
  int i; 
 
  for(i=0; i<100; i++) { 
    if((str[i]=(char *)malloc(128))==NULL) { 
      printf("Allocation error."); 
      exit(0); 
    } 
    gets(str[i]); 
  } 
 
  /* now free the memory */ 
  for(i=0; i<100; i++) free(str[i]); 
 
  return 0; 
}

listing 13
if(heapcheck() == _HEAPOK) 
  printf("Heap is correct."); 
else 
  printf("Error in heap.");

listing 14
int status; 
 
heapfillfree(1); 
status = heapcheckfree(1) 
 
if(status == _HEAPOK) 
  printf("Heap is filled correctly.\n"); 
else 
  if(status == _BADVALUE) 
    printf("Heap not filled with correct value.\n");

listing 15
#include <stdio.h> 
#include <stdlib.h> 
#include <alloc.h> 
 
int main(void) 
{ 
  char *ptr; 
  int status; 
 
  if((ptr = (char *) malloc(10)) == NULL) 
    exit(1); 
 
  status = heapchecknode(ptr); 
 
  if(status == _USEDENTRY) 
    printf("Node is being used.\n"); 
  else 
    printf("Error in heap.\n"); 
 
  free(ptr); 
 
  return 0; 
}

listing 16
int status; 
 
status = heapfillfree(0); 
if(status == _HEAPOK) 
  printf("Heap is correct."); 
else 
  printf("Error in heap.");

listing 17
struct heapinfo { 
  void *ptr; /* pointer to block */ 
  void *ptr2;/* pointer to block */ 
  unsigned int size; /* size of block, in bytes */ 
  int in_use; /* set if block is in use */ 
};

listing 18
typedef struct _heapinfo { 
  int *_pentry; /* pointer to block */ 
  int *__pentry;/* pointer to block */ 
  size_t _size; /* size of block */ 
  int _useflag; /* contains _USEDENTRY if block is in use -- 
                   contains _FREEENTRY if not in use */ 
} _HEAPINFO;

listing 19
#include <stdio.h> 
#include <stdlib.h> 
#include <alloc.h> 
 
int main(void) 
{ 
  struct heapinfo hinfo; 
  char *p1, *p2; 
 
  if((p1 = (char *) malloc(80)) == NULL) 
    exit(1); 
 
  if((p2 = (char *) malloc(20)) == NULL) 
    exit(1); 
 
  if(heapcheck() < 0) { /* always check heap before walk */ 
    printf("Heap corrupt."); 
    exit(1); 
  } 
 
  hinfo.ptr = NULL;  /* set ptr to null before first call */ 
 
  /* examine first block */ 
  if(heapwalk(&hinfo) == _HEAPOK) 
    printf("Size of p1's block is %d\n", hinfo.size); 
 
  /* examine second block */ 
  if(heapwalk(&hinfo) == _HEAPOK) 
    printf("Size of p2's block is %d\n", hinfo.size); 
 
  free(p1); 
  free(p2); 
 
  return 0; 
}

listing 20
#include <stdlib.h> 
 
struct addr { 
  char name[40]; 
  char street[40]; 
  char city[40]; 
  char state[3]; 
  char zip[10]; 
}; 
/* ... */ 
struct addr *get_struct(void) 
{ 
  struct addr *p; 
 
  if(!(p=(struct addr *)malloc(sizeof(addr)))) { 
    printf("Allocation error."); 
    exit(0); 
  } 
  return p; 
}

listing 21
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
 
int main(void) 
{ 
  char *p; 
 
  p = (char *) malloc(17); 
  if(!p) { 
    printf("Allocation error."); 
    exit(1); 
  } 
 
  strcpy(p, "This is 16 chars"); 
 
  p = (char *) realloc(p,18); 
  if(!p) { 
    printf("Allocation error."); 
    exit(1); 
  } 
 
  strcat(p, "."); 
  printf(p); 
  free(p); 
 
  return 0; 
}

⌨️ 快捷键说明

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