Skip to content

Commit bca8306

Browse files
Merge pull request #6161 from oldnewthing/oldnewthing-clistbox-initstorage
Clarify CListBox::InitStorage parameters
2 parents b1a5bca + b754c12 commit bca8306

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

docs/mfc/codesnippet/CPP/clistbox-class_23.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Initialize the storage of the list box to be 256 strings with
2-
// about 10 characters per string, performance improvement.
3-
int n = m_myListBox.InitStorage(256, 16 * sizeof(TCHAR));
1+
// Reserve space in the list box for 256 additional strings with
2+
// about 15 characters per string.
3+
int n = m_myListBox.InitStorage(256, 256 * (15 + 1) * sizeof(TCHAR));
44
ASSERT(n != LB_ERRSPACE);
55

66
// Add 256 items to the list box.

docs/mfc/reference/clistbox-class.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,20 +873,22 @@ int InitStorage(
873873
### Parameters
874874
875875
*`nItems`*<br/>
876-
Specifies the number of items to add.
876+
Specifies the number of items to for which to reserve space.
877877
878878
*`nBytes`*<br/>
879-
Specifies the amount of memory, in bytes, to allocate for item strings.
879+
Specifies the amount of additional memory, in bytes, to allocate for item strings.
880880
881881
### Return Value
882882
883883
If successful, the maximum number of items that the list box can store before a memory reallocation is needed, otherwise `LB_ERRSPACE`, meaning not enough memory is available.
884884
885885
### Remarks
886886
887-
Call this function before adding a large number of items to a `CListBox`.
887+
You can call this function before adding a large number of items to a `CListBox`.
888888
889-
This function helps speed up the initialization of list boxes that have a large number of items (more than 100). It preallocates the specified amount of memory so that subsequent [`AddString`](#addstring), [`InsertString`](#insertstring), and [`Dir`](#dir) functions take the shortest possible time. You can use estimates for the parameters. If you overestimate, some extra memory is allocated; if you underestimate, the normal allocation is used for items that exceed the preallocated amount.
889+
This function helps speed up the initialization of list boxes that have a large number of items (more than 100). It preallocates the specified amount of memory so that subsequent [`AddString`](#addstring), [`InsertString`](#insertstring), and [`Dir`](#dir) functions are more efficient. You can use estimates for the parameters. If you overestimate, the extra memory remains allocated; if you underestimate, the list box will allocate additional memory as necessary.
890+
891+
The memory required to store a string includes the null terminator. Therefore, if you plan to add 100 strings, each with a length of 10 characters, you would pass a *wParam* of 100 and an *lParam* of 100 &times; (10 + 1) &times; sizeof(TCHAR).
890892
891893
Windows 95/98 only: The *`nItems`* parameter is limited to 16-bit values. This means list boxes cannot contain more than 32,767 items. Although the number of items is restricted, the total size of the items in a list box is limited only by available memory.
892894

0 commit comments

Comments
 (0)