gcc 4.7.2
c89
Hello,
The function GetCallRef(…) expect a pointer to a buffer for this 3rd parameter i.e. call_id is my pointer. However, I don’t know the length of the string that will be returned in the call_id. This call_id will only be used in this function, so will only have local scope so I will be only using the call_id returned in my function.
So my question would be; is it ok to declare a pointer and pass that.
char *call_id = NULL;
Or would be be better to declare an array of a very large size (as I don’t know big is big enough)
char call_id[1024]
The actual function.
if(GetCallRef(crn, GET_CALLID, call_id) == 0) {
/* use the call id - not used outside of this function */
}
I don’t have the source code for the actual function GetCallRef(...) because it is closed.
I guess the buffer will be better as the function internally may copy directly into that buffer. However, as arrays decay into pointers in function arguments and I am not passing the size as it is not part of this function signature, it will be just 4 bytes in length. How does the function know how big my array is? It cannot.
If I pass a pointer it can assign the address of where the string value is stored i.e. and return that.
call_id = call_reference_id;
All the document says that the buffer should be big enough to store the call id, but it doesn’t say how big it should be.
Many thanks for any advice,
If the documentation says “buffer should be big enough to store the call id”, it means that the function expects the caller to pass a pre-allocated buffer be it on stack or heap.
The documentation of the function should mention this!
There is no way to be 100% safe without the function telling you this information.
Perhaps you need to check some other api which takes similar parameter or check functional spec which documents this.
Your last option if everything fails is to allocate an array of a big enough size, perhaps
1024and test the code rigorously.