C# allows to mark function argument as output only:
void func(out int i)
{
i = 44;
}
Is it possible to do something similar in C/C++? This could improve optimization. Additionally is should silence out warnings "error: 'myVar' may be used uninitialized in this function", when variable is not initialized and then passed to function as output argument.
I use gcc/g++ (currently 4.4.7) to compile my code.
Edit: I know about pointers and references, this is not what I am looking for. I need something like this:
void func(int* __attribute__((out)) i)
{
*i = 44;
}
void func2()
{
int myVal; // gcc will print warning: 'myVar' may be used uninitialized in this function
func(&myVal);
//...
}
Edit 2: Some extra code is needed to reproduce warning "'myVar' may be used uninitialized in this function". Additionally you have to pass -Wall -O1 to gcc.
void __attribute__((const)) func(int* i)
{
*i = 44;
}
int func2()
{
int myVal; // warning here
func(&myVal);
return myVal;
}
void func(int* i){*i = 44;}
(C)? – Passible-Wall -Wextra -pedantic
flags and even ifmyVal
is used infunc2
. I'm assuming that you meant to pass the address ofmyVal
intofunc
. – Maghutte__attribute__((const))
, which promises that the function "does not examine any values except its arguments, and has no effects except the return value", you now complain that the compiler is fooled by the lie? – Stegall