E
E
Exvel2014-09-17 16:59:55
C++ / C#
Exvel, 2014-09-17 16:59:55

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

3 answer(s)
J
jcmvbkbc, 2014-09-17
@exvel

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.

D
Denis Morozov, 2014-09-17
@morozovdenis

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);
}

D
Don Kaban, 2014-09-17
@donkaban

Slightly to the side. What happens if you pass a long[size] buffer written on a big-endian machine, and you read it element by element... okay, right? If you're writing a library, then a serialization atom is still a byte. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question