passing uninitialized parameters

M

Michael

Hi,

once I read here that it is not 'a good idea'
to pass variables that are not initialized to
a function.

I have
void something ( double *vector );

....
int main ( void )
{
double vector[3];

something ( vector );
...
}

'something' puts its results in the array 'vector'.
Do I have to initialize vector before passing it?
I wonder because if i don't do do, 'splint' blames me of
being lazy ...

Thanks,

Michael
 
J

Jens.Toerring

Michael said:
once I read here that it is not 'a good idea'
to pass variables that are not initialized to
a function.
I have
void something ( double *vector );
...
int main ( void )
{
double vector[3];
something ( vector );
...
}
'something' puts its results in the array 'vector'.
Do I have to initialize vector before passing it?

No, 'vector' is initialized, only the elements of vector aren't.
What arrives in the function is a well-initialized pointer to
the first element of the array you defined in main() and you can
use that to set the elements. I guess the warning was more about
something like

int main( void );
{
double *vector;
something( vector );
...
}

That wouldn't make any sense - if you use the (uninitialized) value
of this 'vector' in the function hell would break loose, and if you
assign a value to 'vector' in the function it won't be visible in
the caller, so passing the argument is useless.

Regards, Jens
 
D

Dan Pop

In said:
once I read here that it is not 'a good idea'
to pass variables that are not initialized to
a function.

Indeed, it's an attrocious idea, resulting in undefined behaviour most of
the time.
I have
void something ( double *vector );

...
int main ( void )
{
double vector[3];

something ( vector );
...
}

'something' puts its results in the array 'vector'.

This is not an example of passing an uninitialised variable to a
function. You're passing the address of vector[] to something(), not
its uninitialised contents.
Do I have to initialize vector before passing it?

No, as long as it is used only for output purposes (i.e. the function
completely ignores its current contents).
I wonder because if i don't do do, 'splint' blames me of
being lazy ...

splint doesn't know whether vector[] is passed to something() for input
or output purposes. If it's passed for input purposes, your code is
wrong, otherways it isn't.

Other languages remove this ambiguity, by allowing the programmer to
specify, in the function "prototype", which arguments are input arguments
and which are output arguments. C doesn't, hence splint's dilemma.

Dan
 
F

Flash Gordon

Hi,

once I read here that it is not 'a good idea'
to pass variables that are not initialized to
a function.

You should pass either an initialised variable or something that is not
a variable.
I have
void something ( double *vector );

...
int main ( void )
{
double vector[3];

something ( vector );
...
}

'something' puts its results in the array 'vector'.
Do I have to initialize vector before passing it?

Here you are passing a *pointer* to the first element of vector. In
other words you are effectively passing the address of vector. This
obviously is not a variable, although it might not be constant if it is
an automatic object (which it is in your example). So, if the definition
of function"something" is such that it is guaranteed to no read any
location that it has not written to then there is no need to initialise
vector. In fact, I would often consider it to be a bad thing since
instead of vector you might do

int main ( void )
{
double vector[1000];

something ( vector );
/* ... */
}

Obviously in this instance initialising vector would be rather
inefficient if the initialisation was not required.
I wonder because if i don't do do, 'splint' blames me of
being lazy ...

This is one of the occasions when you need to think about whether the
warning from splint is really a problem.

By asking the question rather than blindly shutting splint up you are
*definitely* doing the right thing.
 
C

CBFalconer

Flash said:
.... snip ...

This is one of the occasions when you need to think about whether
the warning from splint is really a problem.

By asking the question rather than blindly shutting splint up you
are *definitely* doing the right thing.

The purpose of splint and lint is not to point out errors (although
it may), but to point out areas that require examination. It will
usually tell you why it is suspicious.
 
M

Michael

Hi,

thank you all for these answers. Thats what I want
to know.

Bye, and have a nice December

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top