Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3021

MSDN code is incorrect?

$
0
0
Hi, ALL,
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
}

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.

Viewing all articles
Browse latest Browse all 3021