Archive for the ‘GetSize’ Tag

CObjArray::SetSize with nGrowBy parameter, does not cause array to grow   Leave a comment

  • Calling CObjArray::SetSize, with nGrowBy parameter > 0, does not cause the array to grow.
  • This isn’t a bug, or a documentation error, its a complaint about the lack of documentation for CObjArray::SetSize. The documentation is misleading.
  • In my opinion, unless you read the documentation for SetAt or SetAtGrow, passing an nGrowBy parameter to SetSize would seem to indicate that it causes the array to grow dynamically as needed, by the specified amount. This is not the case.
  • The MSDN documentation does explain this, however, the real explanation is in the documentation for SetAt and SetAtGrow, not in the documentation for SetSize. I have a problem with this. You have to read the documentation for SetAt or SetAtGrow to figure out how the nGrowBy argument supplied to SetSize works.
  • For example, you cannot call myArray.SetSize( 0, 200 ) then call myArray.SetAt( 5, &myValue ). You must use SetAtGrow.
  • This effects all CObjArray related classes, such as CPtrArray.

MSDN documentation for CObjArray::SetSize nGrowBy parameter:

“The nGrowBy parameter affects internal memory allocation while the array is growing. Its use never affects the array size as reported by GetSize and GetUpperBound.”

MSDN documentation for CObjArray::SetAtGrow:

SetAt will not cause the array to grow. Use SetAtGrow if you want the array to grow automatically.

SetAt example (showing incorrect usage):

CPtrArray myArray;
int myValue = 0;
myArray.SetSize( 0, 200 );
myArray.SetAt( 5, &myValue ); // asserts in debug

SetAtGrow example:

CPtrArray myArray;
int myValue = 0;
myArray.SetSize( 0, 200 );
myArray.SetAtGrow( 5, &myValue ); // works


Posted October 2, 2011 by Ed Nafziger in MFC

Tagged with , , , , , ,


Get every new post delivered to your Inbox.

%d bloggers like this: