Hi, ALL,
The code sample at this link contains an error:
The numColumn variable is set to 0 and it is local. Then the value of 0 is used in the malloc call, which creates an array of 0 length in the next 2 lines.
I think the call to SQLNumResultCols() should be done before the calls to malloc().
Any idea why it was done like this?
Thank you.
The code sample at this link contains an error:
Code:
void printStatementResult(SQLHSTMT hstmt)
{
int bufferSize = 1024, i;
SQLRETURN retCode;
SQLSMALLINT numColumn = 0, bufferLenUsed;
SQLPOINTER* columnLabels = (SQLPOINTER *)malloc( numColumn * sizeof(SQLPOINTER*) );
struct DataBinding* columnData = (struct DataBinding*)malloc( numColumn * sizeof(struct DataBinding) );
retCode = SQLNumResultCols(hstmt, &numColumn);
printf( "Columns from that table:\n" );
for ( i = 0 ; i < numColumn ; i++ )
{
columnLabels[i] = (SQLPOINTER)malloc( bufferSize*sizeof(char) );
retCode = SQLColAttribute(hstmt, (SQLUSMALLINT)i + 1, SQL_DESC_LABEL, columnLabels[i], (SQLSMALLINT)bufferSize, &bufferLenUsed, NULL);
wprintf( L"Column %d: %s\n", i, (wchar_t*)columnLabels[i] );
}
// more code
}
I think the call to SQLNumResultCols() should be done before the calls to malloc().
Any idea why it was done like this?
Thank you.