2. Embedded SQL for C : Dynamic Programming for C : How to Declare an SQLDA Variable : Dynamic Allocation of an SQLDA
 
Share this page                  
Dynamic Allocation of an SQLDA
In order to dynamically allocate an SQLDA, you must call an allocation routine (such as the C calloc function) and cast the result as a pointer to an SQLDA. The allocation call must include one header (IISQDA_HEAD_SIZE) and any number of variables (N * IISQDA_VAR_SIZE). For example, the following program fragment dynamically allocates an SQLDA with number variables, and points the variable sqlda at the allocated memory. As soon as the SQLDA is allocated, the sqln field is set to record how many array elements were allocated:
exec sql include sqlda;
IISQLDA *sqlda; /* Pointer to an SQLDA */
...
/*
** 'number' has been assigned a positive number.
** Note that the result of the allocation call, calloc,
** is cast to be a pointer to an SQLDA.
** The calloc routine is passed 2 parameters
** (number of objects, and size of a single object).
*/
sqlda = (IISQLDA *)calloc(1, IISQDA_HEAD_SIZE +
       (number * IISQDA_VAR_SIZE));
if (sqlda == (IISQLDA *)0) /* Memory error */
{
    /* Print error and exit */
    err_exit("Failure allocating %d SQLDA elements\n",
           number);
 }
sqlda->sqln = number; /* Set the size */
...
exec sql describe s1 into :sqlda;
If you change the above allocation call to:
sqlda = (IISQLDA *)calloc(1, sizeof(IISQLDA));
then IISQ_MAX_COLS elements is allocated. This number of elements is the current maximum for data retrievals. In this case, the sqln field should be set to IISQ_MAX_COLS.