I am working with an API wherein the caller passes in an array of pointers to stuff, as well as the length of the array. To make my code more readable/maintainable, I would like to be able to effectively give names to each argument rather than refer to them as arg[0]
, arg[1]
, etc. Is it safe to declare references to all of the possible arguments even if the length of the passed-in array can be different sizes due to optional arguments?
I am trying to do something like this:
void myFunc(int out_args[], size_t nargs) {
int &foo = out_args[0];
int &bar = out_args[1]; // bar is optional argument. is this safe?
...
foo = 5;
if(2 >= nargs)
bar = 10;
...
}
Note that the arguments are output arguments, so I really want to have references to them. So, is it safe to have a dangling-ish reference to args[1] if I never actually use it?
My guess is that this is safe because I imagine the way references are implemented is to treat &
in the variable declaration of references as * const
, and whenever I use the references, then the compiler automatically dereferences the pointers for me. Ie, under the hood, I imagine that what I wrote is translated to something like
void myFunc(int out_args[], size_t nargs) {
int *const foo = &out_args[0];
int *const bar = &out_args[1]; // bar is optional argument. is this safe?
...
*foo = 5;
if(2 >= nargs)
*bar = 10;
...
}
In this case, I believe the code never actually accesses memory it shouldn't, so if the version above is equivalent to this, then I should be ok, right?
EDIT: I'm basically writing a plug in, and the API I'm using and can't do anything about can call my code with either something like
int ret_vals[1]; // only care about 1 return value
myFunc(ret_vals, 1);
or
int ret_vals[2]; // care about both return values
myFunc(ret_vals, 2);
or even
myFunc(NULL, 0); // ignore all return values; just marvel at the side effects
and my code needs to work in all cases.