Condividi tramite


_Alloca( ) API Library Routine

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. */

Remarks

_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.

Warning

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.

Example

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

SET LIBRARY TO ALLOCA  
x = xREPLICATE("x", 120)
? x
? LEN(x)

C 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

   _RetChar(rep);
}

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

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

See Also

Concepts

API Library Routines A-Z

Reference

_AllocHand( ) API Library Routine

_FreeHand( ) API Library Routine

REPLICATE( ) Function

Other Resources

Accessing the Visual FoxPro API

API Library Routines by Category