示例#1
0
文件: malloctest.c 项目: marcov/misc
int 
main(void)
{
	char * b = NULL;
	
	// This fails because allocate1 changes the _value_ of the passed parameter,
	// not the value pointed by the passed parameter.
	allocate1(b);
	printf("Allocate 1: %s\n", b);
	
	b = NULL;
	
	// This is OK, because the allocate2 changes the value _pointed_ by the passed parameter.
	allocate2(&b);
	printf("Allocate 2: %s\n", b);
	
	b = NULL;
}
int main (int argc, char const *argv[])
{
  int * ptr1;
  int * ptr2;
  bool allocate1Success = false;
  bool allocate2Success = false;
  
  try
  {
    allocate1(10, ptr1);
    allocate1Success = true;
  }
  catch(std::bad_alloc &e)
  {
  }
  
  if (allocate1Success)
    delete [] ptr1;
  
  allocate2(10, ptr1, ptr2);
  
  /* code */
  return 0;
}