www.digitalmars.com         C & C++   DMDScript  

D - passing null to an out parameter

reply imr1984 <imr1984_member pathlink.com> writes:
ok i want to do something like this:

void setInt(out int myInt)
{
if(&myInt!= null)
myInt = 0xfoobar;
}

int main()
{
int x;
setInt(x);
setInt(null);//this requires a cast to do such as *(int*)null

return 0;
}

Can we please be allowed to pass null to an out parameter if we dont need it?
Apr 29 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
imr1984 wrote:

ok i want to do something like this:

void setInt(out int myInt)
{
if(&myInt!= null)
myInt = 0xfoobar;
}

int main()
{
int x;
setInt(x);
setInt(null);//this requires a cast to do such as *(int*)null

return 0;
}

Can we please be allowed to pass null to an out parameter if we dont need it?


  

going to use null, you might as well use pointers. Simply using a different name for a pointers is not the way avoid pointer use. In your example, if you pass in a null, it will make no difference with out because it makes a new out variable anyway. -- -Anderson: http://badmama.com.au/~anderson/
Apr 29 2004
parent reply imr1984 <imr1984_member pathlink.com> writes:
ok so say you have a function that optionally returns a value through a passed
pointer. If you declare a variable to store that returned value, dmd will
generate automatically the initialising code for that storage variable, even
though it will just be a waste of time; because dmd cant know that the function
doesnt read the value, it just writes to it. Whereas if you could pass null to
out parameters then this overhead would be avoided. This is why it is a good
idea to allow null to be passed to an out param.


In article <c6qv67$16mf$1 digitaldaemon.com>, J Anderson says...
imr1984 wrote:

ok i want to do something like this:

void setInt(out int myInt)
{
if(&myInt!= null)
myInt = 0xfoobar;
}

int main()
{
int x;
setInt(x);
setInt(null);//this requires a cast to do such as *(int*)null

return 0;
}

Can we please be allowed to pass null to an out parameter if we dont need it?


  

going to use null, you might as well use pointers. Simply using a different name for a pointers is not the way avoid pointer use. In your example, if you pass in a null, it will make no difference with out because it makes a new out variable anyway. -- -Anderson: http://badmama.com.au/~anderson/

Apr 29 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
imr1984 wrote:

ok so say you have a function that optionally returns a value through a passed
pointer. If you declare a variable to store that returned value, dmd will
generate automatically the initialising code for that storage variable, even
though it will just be a waste of time; because dmd cant know that the function
doesnt read the value, it just writes to it. Whereas if you could pass null to
out parameters then this overhead would be avoided. This is why it is a good
idea to allow null to be passed to an out param.
  

If your not going to put anything into the out of the function you might as well use void setInt(out int myInt) { myInt = 0xfoobar; } void setInt() //Null version { //Do nothing } Apart from forcing the programmer to test for null in every function (for a *correct program design*), testing the variable in many cases could be more expensive then this allocation. Personally I think the compiler should runtime error when you de-reference a null variable (like in C++). I'm going to partition to have that put in. -- -Anderson: http://badmama.com.au/~anderson/
Apr 29 2004
prev sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
"imr1984" <imr1984_member pathlink.com> wrote in message
news:c6qni2$rdc$1 digitaldaemon.com...
 ok i want to do something like this:

 void setInt(out int myInt)
 {
 if(&myInt!= null)
 myInt = 0xfoobar;
 }

 int main()
 {
 int x;
 setInt(x);
 setInt(null);//this requires a cast to do such as *(int*)null

 return 0;
 }

 Can we please be allowed to pass null to an out parameter if we dont need it?

No. An out parameter is part of the decision of the designer of a given class/function. They know better than anyone, including you, whether or not an out param is necessitated.
Apr 30 2004