Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++: Uninitialised Variable Passed as a Parm

Reply
Thread Tools

C++: Uninitialised Variable Passed as a Parm

 
 
Gene Wirchenko
Guest
Posts: n/a
 
      12-17-2003
Is the following guaranteed safe?

void InitInt(int & SomeInt)
{
SomeInt=3;
return;
}

int main()
{
int MainInt; // not initialised!
InitInt(MainInt);
std::cout << MainInt << std::endl;
return 0;
}

The intent of the function is to initialise its parm, so why
bother initialising it first in main()? Unless, of course, it is not
safe. Is it safe?

Sincerely,

Gene Wirchenko

 
Reply With Quote
 
 
 
 
Gene Wirchenko
Guest
Posts: n/a
 
      12-17-2003
On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
<> wrote:

> Is the following guaranteed safe?


Oops! I forgot to
#include <iostream>

> void InitInt(int & SomeInt)
> {
> SomeInt=3;
> return;
> }
>
> int main()
> {
> int MainInt; // not initialised!
> InitInt(MainInt);
> std::cout << MainInt << std::endl;
> return 0;
> }
>
> The intent of the function is to initialise its parm, so why
>bother initialising it first in main()? Unless, of course, it is not
>safe. Is it safe?


Sincerely,

Gene Wirchenko

 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      12-17-2003

"Gene Wirchenko" <> schrieb im
Newsbeitrag news:...
> On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
> <> wrote:
>
> > Is the following guaranteed safe?

>
> Oops! I forgot to
> #include <iostream>
>
> > void InitInt(int & SomeInt)
> > {
> > SomeInt=3;
> > return;
> > }
> >
> > int main()
> > {
> > int MainInt; // not initialised!
> > InitInt(MainInt);
> > std::cout << MainInt << std::endl;
> > return 0;
> > }
> >
> > The intent of the function is to initialise its parm, so why
> >bother initialising it first in main()? Unless, of course, it is not
> >safe. Is it safe?


In principle it´s safe though the practice itself is arguable - why not
initialize at the point of declaration?
To see whether it makes sense you´d need to provide some more details of
your intention.

Chris


 
Reply With Quote
 
Le Géant Vert
Guest
Posts: n/a
 
      12-17-2003

"Gene Wirchenko" <> a écrit dans le
message de news: ...
> On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
> <> wrote:
>
> > Is the following guaranteed safe?

>
> Oops! I forgot to
> #include <iostream>
>
> > void InitInt(int & SomeInt)
> > {
> > SomeInt=3;
> > return;
> > }
> >
> > int main()
> > {
> > int MainInt; // not initialised!
> > InitInt(MainInt);
> > std::cout << MainInt << std::endl;
> > return 0;
> > }
> >
> > The intent of the function is to initialise its parm, so why
> >bother initialising it first in main()? Unless, of course, it is not
> >safe. Is it safe?

>
> Sincerely,
>
> Gene Wirchenko
>


sounds perfectly safe to me... nevertheless, I hardly understatnd the point
of this function : costs a function call and does almost nothing
interesting... and you don't need the return in the function InitInt.


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      12-17-2003
On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
<> wrote:

> Is the following guaranteed safe?
>
> void InitInt(int & SomeInt)
> {
> SomeInt=3;
> return;
> }
>
> int main()
> {
> int MainInt; // not initialised!
> InitInt(MainInt);
> std::cout << MainInt << std::endl;
> return 0;
> }
>
> The intent of the function is to initialise its parm, so why
>bother initialising it first in main()? Unless, of course, it is not
>safe. Is it safe?


Yes - you don't convert MainInt to an rvalue until after you've given
it a valid value.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-17-2003
"Le Géant Vert" <_> wrote...
>
> "Gene Wirchenko" <> a écrit dans le
> message de news: ...
> > On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
> > <> wrote:
> >
> > > Is the following guaranteed safe?

> >
> > Oops! I forgot to
> > #include <iostream>
> >
> > > void InitInt(int & SomeInt)
> > > {
> > > SomeInt=3;
> > > return;
> > > }
> > >
> > > int main()
> > > {
> > > int MainInt; // not initialised!
> > > InitInt(MainInt);
> > > std::cout << MainInt << std::endl;
> > > return 0;
> > > }
> > >
> > > The intent of the function is to initialise its parm, so why
> > >bother initialising it first in main()? Unless, of course, it is not
> > >safe. Is it safe?

> >
> > Sincerely,
> >
> > Gene Wirchenko
> >

>
> sounds perfectly safe to me... nevertheless, I hardly understatnd the

point
> of this function : costs a function call and does almost nothing
> interesting... and you don't need the return in the function InitInt.


Gene probably provided the simple function just for illustration purposes.
The point is that if you pass an uninitialised object by reference, some
compilers complain (with a warning, of course), often such warning is not
necessarily founded. That's all.

Victor


 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      12-17-2003
On Wed, 17 Dec 2003 19:39:13 +0100, "Le Géant Vert"
<_> wrote:

[snip]

>sounds perfectly safe to me... nevertheless, I hardly understatnd the point
>of this function : costs a function call and does almost nothing
>interesting... and you don't need the return in the function InitInt.


Thank you. As to the nit:

Oh, come off it! I posted a small program that illustrated my
point. It is called an example. If the initialisation function had
included code to initialise a 79 TB structure, it would not have made
the point any better (and would probably have obscured it).

Sincerely,

Gene Wirchenko

 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      12-17-2003
On Wed, 17 Dec 2003 18:46:30 +0000, tom_usenet
<> wrote:

>On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
><> wrote:


[snip]

>> The intent of the function is to initialise its parm, so why
>>bother initialising it first in main()? Unless, of course, it is not
>>safe. Is it safe?

>
>Yes - you don't convert MainInt to an rvalue until after you've given
>it a valid value.


Thank you.

Yes, very deliberately so.

(I have encountered languages where this would not be safe.)

Sincerely,

Gene Wirchenko

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      12-17-2003
Gene Wirchenko wrote:

> Is the following guaranteed safe?
>
> void InitInt(int & SomeInt) {
> SomeInt=3;
> return;
> }


This is poor programming practice. The following:

int InitInt(void) {
return 3;
}

would be much better.

>
> int main(int argc, char* argv[]) {
> int MainInt; // not initialized!
> InitInt(MainInt);


int MainInt = InitInt();

You should *always* try to avoid uninitialized variables.

> std::cout << MainInt << std::endl;
> return 0;
> }
>
> The intent of the function is to initialized its parm, so why
> bother initializing it first in main()? Unless, of course, it is not
> safe. Is it safe?


It is *dangerous* because the variable may be used uninitialized.
If not by you, then by some poor programmer who must maintain your code.
Good programming habits will help keep you out of trouble.

 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      12-17-2003

"Gene Wirchenko" <> wrote in message
news:...
> On Wed, 17 Dec 2003 19:39:13 +0100, "Le Géant Vert"
> <_> wrote:
>
> [snip]
>
> >sounds perfectly safe to me... nevertheless, I hardly understatnd the

point
> >of this function : costs a function call and does almost nothing
> >interesting... and you don't need the return in the function InitInt.

>
> Thank you. As to the nit:
>
> Oh, come off it! I posted a small program that illustrated my
> point. It is called an example.


Now I think you understand why I object when people post "answers" that are
off the topic of the original question. Interesting


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pass more than one parm =?Utf-8?B?cm9kY2hhcg==?= ASP .Net 2 02-14-2007 06:28 PM
uninitialised variable but NO error geek.arnuld@gmail.com C++ 2 02-04-2007 11:05 AM
uninitialised variable but NO error geek.arnuld@gmail.com C++ 2 02-04-2007 08:19 AM
How Java Web Start JNLP Codebase parm specify a local file system? cubes Java 2 12-09-2005 08:14 PM
value of an uninitialised variable Andy Fish XML 7 01-10-2005 04:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57