SQL/MP Programming Manual for C

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C429847-008
10-30
Allocate Memory for the SQLDA Structures and
Names Buffers
Example 10-4 shows the allocate_sqlda function, which is also called to allocate
the output SQLDA structure. This function initializes the eye_catcher and ind_ptr
fields.
To allocate memory for the names buffer, call malloc and pass in_nameslen. You
specify an arbitrarily large size for the space required because SQL must have
advance information about the space where to store the names. The program still
allocates only the memory that is actually needed for the names, and SQL ignores any
unused memory.
In this call, input_namesbuf_ptr is a pointer to the memory allocated for an input
names buffer:
typedef char (*arrayptr) [1000];
...
if (in_nameslen > 0)
input_namesbuf_ptr = (arrayptr) malloc(in_nameslen);
Example 10-4. Allocating the SQLDA Structure
/* in main code: */
/* typedef struct SQLDA_TYPE *sqldaptr; */
/* sqlda_type and sqlvar_type are generated by INCLUDE SQLDA
*/
sqldaptr allocate_sqlda (num_entries)
int num_entries; /* number of input or output variables */
{
sqldaptr sqlda_ptr; /* pointer to be returned */
int mem_reqd; /* number of bytes required for SQLDA
short i; /* loop counter */
sqlda_ptr = NULL;
mem_reqd = sizeof( struct SQLDA_TYPE ) +
( (num_entries - 1) * sizeof( struct SQLVAR_TYPE ) );
...
/* call malloc to allocate memory (error checking omitted */
sqlda_ptr = (sqldaptr)malloc (mem_reqd);
sqlda_ptr->num_entries = num_entries;
...
/* Initialize eye_catcher and ind_ptr */
...
/* return the pointer to newly allocated memory: */
return(sqlda_ptr);
}