freestor.cpp

来自「不错的国外的有限元程序代码,附带详细的manual,可以节省很多的底层工作.」· C++ 代码 · 共 81 行

CPP
81
字号
//   file FREESTOR.C

#include "freestor.h"
#include "compiler.def"
#include "debug.def"
#include <stdio.h>
#include <stdlib.h>


double*  allocDouble (int n)
   // Allocates and returns an array of n doubles. Issues an error message if
   // the free store is exhausted.
{
   double* answer ;

#  ifdef DEBUG
      if (! n) {
	 printf ("cannot allocate 0 coefficients in fucntion allocDouble\n");
	 exit(0) ;}
#  endif

   answer = (double*) calloc(n,sizeof(double)) ;
   if (answer)
      return answer ;
   else
      freeStoreError() ;
   return NULL;
}


int*  allocInt (int n)
   // Allocates and returns an array of n integers. Issues an error message
   // if the free store is exhausted.
{
   int* answer ;

#  ifdef DEBUG
      if (! n) {
	 printf ("cannot allocate 0 coefficients in function 'allocInt'\n") ;
	 exit(0) ;}
#  endif

   answer = (int*) calloc(n,sizeof(int)) ;
   if (answer)
      return answer ;
   else
      freeStoreError() ;
   return NULL;
}


void  freeStoreError ()
   // This function is called whenever operator "new" is unable to allocate 
   // memory.
{
   printf ("Sorry : free store exhausted \n") ;
   exit(0) ;
}


void  freeDouble (double* a)
   // Deallocates the list of decimals 'a'.
{
#ifdef SUN_STATION
   free ((char*)a) ;        /* the Sun compiler is really lousy */
#else
   free (a) ;
#endif
}


void  freeInt (int* a)
   // Deallocates the list of integers 'a'.
{
#ifdef SUN_STATION
   free ((char*)a) ;        /* see above */
#else
   free (a) ;
#endif
}

⌨️ 快捷键说明

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