/* alloca() This small program that is furnished by IBM is a simple example to provide an illustration. This example has not been thoroughly tested under all conditions. IBM, therefore, cannot guarantee or imply reliability, serviceability, or function of this program. All programs contained herein are provided to you "AS IS". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. */ /*--------------------------------------------------------------------*/ /* */ /* Example Group: Program Execution */ /* */ /* Function: modasa (Modify Automatic Storage Area) */ /* replaces alloca() to allocate from automatic */ /* storage, rather than from the heap. */ /* */ /* Description: This simple example shows how the automatic */ /* storage frame (ASF) can be extended. */ /* */ /*--------------------------------------------------------------------*/ #include #include #define alloca(x) modasa(x) char *ptr; /* A pointer that will be set */ /* to the location of the first */ /* byte of the extended area. */ /* The size by which to extend */ int additional_bytes =32000; /* the automatic storage frame. */ /*********************************************************************/ /* If the function allocated from heap, then the total allcation */ /* would continue to grow in size. Because the storage is freed */ /* when huh() returns, the total allocation does not grow with */ /* each call to huh(). */ /*********************************************************************/ void huh(void); void main() { int i; for (i=0; i<1000; i++) { huh(); } sleep(20); } void huh() { ptr = alloca( additional_bytes ); /* do some stuff with the space... just because it's there... */ strcpy( ptr, "Hello there! " ); printf("%s", ptr); }