Allocate memory for column data

The sqlda structure stores a pointer to the data for each column in the sqldata field of an sqlvar_struct structure. Unlike the DESCRIBE...USING SQL DESCRIPTOR statement, the DESCRIBE...INTO statement does not allocate memory for this data. When the DESCRIBE...INTO statement allocates memory for the sqlda pointer, it initializes the sqldata fields of each sqlvar_struct structure to null.

To send or receive column data in the database, your program must perform the following tasks:
  • Allocate memory for the column data.
  • Set the sqldata field for the sqlvar_struct structure associated with the column to the address of the memory allocated for the column data.
To allocate memory for the sqldata fields, you can use a system memory-allocation function such as malloc() or calloc(). As an alternative to the malloc() system memory-allocation function, your program can declare a static character buffer for the data buffer. The following figure shows a code fragment that allocates column data from a static character buffer called data_buff.
Figure 1: Allocating column data from a static character buffer
static char data_buff[1024];
struct sqlda *sql_descp;
struct sqlvar_struct * col_ptr;
short cnt, pos; 
int size;
;

for(col_ptr=sql_descp->sqlvar, cnt=pos=0; cnt < sql_descp->sqld; 
              cnt++, col_ptr++)
   {
   pos = (short)rtypalign(pos, col_ptr->sqltype);
   col_ptr->sqldata = &data_buf[pos];
   size = rtypmsize(col_ptr->sqltype, col_ptr->sqllen);
   pos += size;
   }

You can replace the code fragment in Allocating column data from a static character buffer with a series of system memory-allocation calls within the for loop. However, system memory-allocation calls can be expensive so it is often more efficient to have a single memory allocation and then align pointers into that memory area.

When you allocate the column data, make sure that the allocated memory is formatted for the column data type. This data type is one of the or SQL data types defined in the sqltypes.h header file. (See Determine the data type of a column.) Make the allocated memory large enough to accommodate the maximum size of the data in the column.

You must also ensure that the data for each column begins on a proper word boundary in memory. On many hardware platforms, integer and other numeric data types must begin on a word boundary. The C language memory-allocation routines allocate memory that is suitably aligned for any data type, including structures, but the routines do not perform alignment for the constituent components of the structure.

Using the proper word boundaries assures that data types are machine independent. To assist you in this task, provides the following memory-management functions:
  • The rtypalign() function returns the position of the next proper word boundary for a specified data type.

    This function accepts two arguments: the current position in the data buffer and the integer or SQL data type for which you want to allocate space.

  • The rtypmsize() function returns the number of bytes of memory that you must allocate for the specified or SQL data type.

    This function accepts two arguments: the integer or SQL data type (in sqltype) and the length (in sqllen) for each column value.

When you allocate memory for the DATETIME or INTERVAL data types, you can take any of the following actions to set the qualifiers in the dtime_t and intrvl_t structures:
  • Use the value that is in the associated sqllen field of sqlda.
  • Compose a different qualifier with the values and macros that the datatime.h header file defines.
  • Set the data type qualifier to 0 and have the database server set this qualifier during the fetch. For DATETIME values, the data type qualifier is the dt_qual field of the dtime_t structure. For INTERVAL values, the data type qualifier is the in_qual field of the intrvl_t structure.

For examples that allocate memory for the sqldata fields, see the demo3.ec and unload.ec demonstration programs that are supplied with .