0

I'm curious of how people design C programs.

When I pass an array to a function I found out that generally people use

void function (unsigned char * ptr, size_t size)

However, I have to pass an array, which size is unknown in compile time, and have to return it after a function. Do I have to do

size_t function (unsigned char * ptr, size_t size)

or

void function (unsigned char * ptr, *size_t size)

?

In the method below, I will pass the size of the original array, and change the array size because I don't need the original size anymore.

I'm curious which is more general or better or safe. (or recommended)

The situation is that I'm doing socket connections and encrypting and decrypting messages. I heard that I should not use strlen() because the messages aren't actually strings.

Jake
  • 9
  • 2
  • Does your function change the size of the array that ptr points to? If not, why are you overwriting that size? – jarmod May 17 '22 at 14:43
  • 1
    You probably want this: `void function (unsigned char **ptr, size_t *size)` or `size_t function (unsigned char **ptr, size_t size)`. Also read this: https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c – Jabberwocky May 17 '22 at 14:43
  • Both methods can work depending on your preferred style or context. Both are safe as long as you write a *correct* code. But in general, passing pointers around might be considered less safe in certain contexts , because it is more error-prone and can lead to various memory corruption schemes. – Eugene Sh. May 17 '22 at 14:44
  • Regarding `strlen`: you can use is on strings AKA null terminated character arrays. – Jabberwocky May 17 '22 at 14:44
  • Are you needing to keep track of three things: the pointer to the array, the size of the array, and the count of items currently stored in that array? – Chris May 17 '22 at 14:49
  • It's unkown at *compile* time – so you cannot do things like `void function(unsigned char(*)[SomeCompileTimeConstant])` – but it seems to be known at *runtime* – that's why you pass the function accepts it as *parameter*, which can at runtime carry arbitrary values. Now if your function does *not* modify the array length then there's no reason to return the size or accept it as pointer. If it *does* then you need to change the pointer having been passed to the function, too (as pointers are passed by value, i.e. copied – thus the `unsigned char**` mentioned already!). – Aconcagua May 17 '22 at 14:53
  • Finally: Let's say the function is intended to *remove* elements from the array by moving those that should remain towards front, overwriting the ones to be deleted (leaving a bunch of outdated values at the very end available for re-use later on, especially: not modifying the array itself, only the contents – or in any other way combine multiple elements to fewer ones) then I'd go with return value – either the number of elements remaining or rather a pointer one past the last element remaining. – Aconcagua May 17 '22 at 14:57
  • If you're changing the array size then your function is mutating the array, and you should pass as `unsigned char**` as suggested earlier. Another alternative is to return the pointer, `unsigned char* function(unsigned char* ptr, size_t* size)`, where `function` will do whatever manipulations on `ptr`, change the `size` accordingly, and return `ptr`. An advantage here is you'd have some error handling baked into the return value (return NULL on error) which would be easier for the caller to check IMO. – yano May 17 '22 at 15:06
  • _"[...] change the array size because I don't need the original size anymore."_ Both work fine. It depends on your preference. But I highly suggest that you use a consistant style across your code. – Zakk May 17 '22 at 15:18

0 Answers0