JavaScript Editor js editor     Web development 

Main Page

Allocates a block of stack space for the calling routine.

void FAR * _Alloca(unsigned int size)
unsigned int size;            /* Size of stack space to allocate in 
bytes. */


_Alloca(В ) returns a pointer to the block if the block is successfully allocated, or returns zero if the block isn't allocated. _Alloca(В ) automatically frees memory when the routine ends, so no corresponding release routine is necessary.

Some C compilers, such as MPW C, do not support memory allocation from the stack; calls to _Alloca() using such compilers will have unpredictable results. Instead, use local variables, or calls to _AllocHand() and _FreeHand(), in your code to reserve memory.

For more information on how to create an API library and integrate it with Visual FoxPro, see Accessing the Visual FoxPro API.


The following example duplicates the action of the Visual FoxPro REPLICATE(В ) function. The memory temporarily used to duplicate the character comes from _Alloca(В ).

Visual FoxPro Code

В Copy Code
x = xREPLICATE("x", 120)
? x
? LEN(x)

C Code

В Copy Code
#include <pro_ext.h>

void FAR allocaEx(ParamBlk FAR *parm)
   char FAR *rep;
   char c = *(char *) _HandToPtr(parm->p[0].val.ev_handle);

   rep = _Alloca((int) parm->p[1].val.ev_long + 1);
   _MemFill(rep, c, (int) parm->p[1].val.ev_long);
  rep[parm->p[1].val.ev_long] = '\0';  // null terminate


FoxInfo myFoxInfo[] =
   {"XREPLICATE", (FPFI) allocaEx, 2, "C,I"},

FoxTable _FoxTable =
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo

See Also

JavaScript Editor js editor     Web development