Answer the question
In order to leave comments, you need to log in
How is it customary to understand the size parameter in C/C++ functions?
C++ is not my native language for me. On duty, I write a DLL in C ++.
Some functions need to accept/return arrays of data or unicode strings.
Since this is a library, the std::veto r and std::wstring types cannot be used in the interface (they are not portable).
I'm using the old C syntax. For example, return of a line: void Foo(wchar_t *buffer, int size) { ... }
Actually a question.
What is commonly understood by the size parameter in the example above: the number of elements in the array or the actual size in bytes?
In the code above, the size in bytes is 2 times larger than the number of elements in the array, so it is advisable not to guess, but to use "like everyone else".
And one more thing, should we use, instead of int , a macrosize_t ? I do not think that it will differ from compiler to compiler, but it will more accurately convey the meaning of the parameter.
Answer the question
In order to leave comments, you need to log in
The size in bytes is used more often when the exchange is unstructured data, or data with variable length elements. Usually a pointer to data is of type void *.
When swapping arrays with fixed-size elements, it's usually more convenient to use the number of elements.
buffer size
int Foo(wchar_t *buffer, int size) { ... }
...
wchar_t buffer[256];
int size = Foo(buffer, 256);
while (size > 0)
{
//пока size больше 0 функция что-то возвращает
size = Foo(buffer, 256);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question