SQL/MP Programming Manual for C

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C429847-008
10-29
Allocate Memory for the SQLDA Structures and
Names Buffers
Allocate Memory for the SQLDA Structures and Names Buffers
To allocate memory for the SQLDA structures and names buffers for the input and
output variables, use the malloc function. The malloc(n) function allocates a block
of memory, n bytes long, and returns the address of that block. The function returns a
pointer to void, which is compatible with any pointer type. However, to enhance
readability, specify the intended type using a cast operator. To include the malloc
function in the stdlibh library, specify:
#include <stdlibh>
The program calls malloc and puts the values returned by malloc into pointers to
the SQLDA structure and names buffer variables defined earlier in the program.
Initialize the eye_catcher and ind_ptr Fields
When you allocate the SQLDA, you must explicitly initialize the eye_catcher and
ind_ptr fields. You must initialize ind_ptr even if your program is not using
indicator variables to handle null values.
When you issue INCLUDE SQLDA to create the SQLDA template, the C compiler
creates a #define for SQLDA_EYE_CATCHER, which you then use to initialize the
eye_catcher field:
sqlda_name.eye_catcher = SQLDA_EYE_CATCHER;
Initialize the ind_ptr and var_ptr fields for each SQLVAR entry to NULL:
for (i = 0; i < num_entries; i++)
sqlda-name -> sqlvar[i].ind_ptr = NULL;
sqlda-name -> sqlvar[i].var_ptr = NULL;
Prepare to Allocate the SQLDA Structure and Names Buffer
In preparation for allocating memory to store the SQLDA structure, get the number of
input parameters or output variables from the SQLSA structure. Similarly, to allocate
memory for the names buffer, get the length of the input or output names buffer from
the SQLSA structure.
num_input_vars = sqlsa.u.prepare.input_num;
if (num_input_vars > 0)
allocate_sqlda(num_input_vars);
...
in_nameslen = sqlsa.u.prepare.input_names_len;
if (in_nameslen > 0)
allocate_namesbuf(in_nameslen);