473,621 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing uninitialized parameters

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

Nov 14 '05 #1
5 3907
Michael <mi****@gmx.net > wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
In <cp**********@r zcomm2.rz.tu-bs.de> Michael <mi****@gmx.net > writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #3
On Fri, 10 Dec 2004 15:46:47 +0100
Michael <mi****@gmx.net > wrote:
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"someth ing" 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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #4
Flash Gordon wrote:
.... 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.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #5
Hi,

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

Bye, and have a nice December

Michael

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
6211
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
6
8220
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the generic pointer type. My real question is, is (void *) such a generic pointer type that it
3
9569
by: julien | last post by:
Hello, Is it possible if a boolean was initialized or not? For other types of variable, I usually check if it is null. But this not possible for a boolean. Thank you Julien
1
13624
by: John Hoge | last post by:
Is it possible to pass a null value to a stored procedure in .net? I have a search Sproc that can take one of two numbers to search on, but not both. I use the code below to pass a null value to the sproc if a TextBox is empty. SqlCommand getLesNumbers = new SqlCommand(); getLesNumbers.CommandText="usp_getLesNumbers"; getLesNumbers.CommandType = CommandType.StoredProcedure; getLesNumbers.Connection = msoConn;
17
2803
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
50
6468
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the union's types can be passed to a function directly -- without creating a separate variable of the union type and assigning the appropriate field of it. Is gcc being too liberal, or is this behavior simply part of a newer
2
2526
by: PSN | last post by:
Hi all .. can any one please explain the output of the following code .. class A { public: int a1; int a2; A(int a1, int a2) { cout << "i am in AAA" << endl;
12
2579
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
2
1145
by: pcaisse | last post by:
Could someone please tell me why this stupid script doesn't print the string being passed to the sub?: #!/usr/bin/perl -w use strict; print "Work, damnit!\n"; my $val = "print me";
0
8213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8156
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8597
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8306
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8457
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4065
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.