I don't quite understand how to call CAtlArray::SetCount.
This is the signature:
bool SetCount(size_t nNewSize, int nGrowBy = - 1);
And here's an explanation of the parameters, from the docs:
nNewSize
The required size of the array.
nGrowBy
A value used to determine how large to make the buffer. A value of -1 causes an internally calculated value to be used.
So, let's say you have an array of some current length, and you'd like to add another 7 units to it. How would you call SetCount? Do you get the current count, add 7, and then pass that number as the first argument? Or do you just pass 7 as the second argument -- and if so, what's the first argument?
CodePudding user response:
The lack of details on the nGrowBy parameter is a hint that Microsoft's guys do not want ordinary programmers to use it. For normal operations you can ignore it and trust the default value of -1 to do the right thing.
So the correct way to add a number of items is what you guessed:
- find the current size if you do not have it at hand
- add the number or units to add
- and pass that value as the first argument of
SetCount(leaving the default value of -1 for the second argument)
CodePudding user response:
nGrowBy sets storage growing strategy.
| nGrowBy | Strategy |
|---|---|
| -1 | Use last set strategy. If not set, default strategy is 0. |
| 0 | Set default strategy which grows storage by at least 50%. |
| >0 | Set new strategy which grows storage by at least nGrowBy items. |
Every time when nNewSize is larger than size calculated by growing strategy, nNewSize is used instead of calculated value (this explains at least in above table).
In most cases you should use just -1 (not specify it at all). Providing explicit value for nGrowBy makes sense only when you know something special on future array resizing, but even then in most cases it is better to just SetCount( final_size ).
