www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - function return value in or out?

reply bobef <bobef_member pathlink.com> writes:
Consider following example:

char[] arr;
char get(int i){return arr[i];}

Is the character returned by get a copy or the original character that can be
modified? And how one can specify desired behavior when there is no in/out for
return value?
Jan 27 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 27 Jan 2005 22:44:53 +0000 (UTC), bobef wrote:

 Consider following example:
 
 char[] arr;
 char get(int i){return arr[i];}
 
 Is the character returned by get a copy or the original character that can be
 modified? And how one can specify desired behavior when there is no in/out for
 return value?
It returns a copy of the value in the array. If you modify the returned value, the array value is *not* modified. What is the "desired behavior" that you are seeking? Are you really wanting the address of the value in the array, or the value itself? -- Derek Melbourne, Australia 28/01/2005 9:49:36 AM
Jan 27 2005
parent reply bobef <bobef_member pathlink.com> writes:
I really need to write something like

char[] sss;
char &get(int i){return sss[i];}

to get the "real" character... And I don't want ot involve pointers...
Because as I undestand D policy, pointers are not "recommended", and goal of D
is to bring things to a higher level.

The actual reason I started this post is because I am trying to write down the
address of a xml node in the lParam of a win32 tree view item, which param is
int. So I need to do some "down and dirty" programming - cast(int)&node, but I
get the address of the reference or something like that and not the real object,
which causes me lot of pain...


In article <ctbrbr$df8$1 digitaldaemon.com>, Derek Parnell says...
On Thu, 27 Jan 2005 22:44:53 +0000 (UTC), bobef wrote:

 Consider following example:
 
 char[] arr;
 char get(int i){return arr[i];}
 
 Is the character returned by get a copy or the original character that can be
 modified? And how one can specify desired behavior when there is no in/out for
 return value?
It returns a copy of the value in the array. If you modify the returned value, the array value is *not* modified. What is the "desired behavior" that you are seeking? Are you really wanting the address of the value in the array, or the value itself? -- Derek Melbourne, Australia 28/01/2005 9:49:36 AM
Jan 28 2005
next sibling parent reply Derek <derek psyc.ward> writes:
On Fri, 28 Jan 2005 13:25:20 +0000 (UTC), bobef wrote:

 char[] sss;
 char &get(int i){return sss[i];}
Does this help ... <code> module test; import std.stdio; char[] sss; char* get(int i){return &(sss[i]);} void main ( ) { char* ac; sss = "QWERTY"; ac = get(1); writefln("%d", cast(uint) ac); writefln("%s", sss); (*ac) = 'Q'; writefln("%s", sss); } </code> -- Derek Melbourne, Australia
Jan 28 2005
parent reply bobef <bobef_member pathlink.com> writes:
I could use pointers of course but it's just not cool...

In article <1k7bq87abdorp.11vg7w3lw2vvb.dlg 40tude.net>, Derek says...
On Fri, 28 Jan 2005 13:25:20 +0000 (UTC), bobef wrote:

 char[] sss;
 char &get(int i){return sss[i];}
Does this help ... <code> module test; import std.stdio; char[] sss; char* get(int i){return &(sss[i]);} void main ( ) { char* ac; sss = "QWERTY"; ac = get(1); writefln("%d", cast(uint) ac); writefln("%s", sss); (*ac) = 'Q'; writefln("%s", sss); } </code> -- Derek Melbourne, Australia
Jan 28 2005
parent reply Sebastian Beschke <s.beschke gmx.de> writes:
bobef schrieb:
 I could use pointers of course but it's just not cool...
 
I guess in this case, pointers are pretty much your only viable option. They've got to have *some* use, you know ;) Thinking about it, you could always call-by-reference. void get(int idx, out char ch) { ch = arr[idx]; } But that's even uglier, IMHO. -Sebastian
Jan 28 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 void get(int idx, out char ch) {
     ch = arr[idx];
 }
That still, unfortunately, doesn't allow you to edit the character in the array. I think you could write.. char[] get(int i) { return arr[i..i+1]; } And it'd return a slice into the original array.
Jan 28 2005
parent reply brad beveridge <brad nowhere.com> writes:
Jarrett Billingsley wrote:
void get(int idx, out char ch) {
    ch = arr[idx];
}
That still, unfortunately, doesn't allow you to edit the character in the array. I think you could write.. char[] get(int i) { return arr[i..i+1]; } And it'd return a slice into the original array.
Isn't there the fundamental problem of array relocation though? What happens if arr gets resized & moved? The character or slice that you just got will not be within the newly moved array. I'm not sure though. Brad
Jan 28 2005
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 Isn't there the fundamental problem of array relocation though?  What 
 happens if arr gets resized & moved?  The character or slice that you just 
 got will not be within the newly moved array.  I'm not sure though.
I tried what you said, and this snippet shows it.. ------------------------------------------------- import std.stdio; import std.gc; void main() { int[] a; a.length=5; for(int i=0; i<5; i++) a[i]=i+1; int[] b=a[3..4]; b[0]=10; for(int i=0; i<5; i++) writefln(a[i]); writefln(cast(int)a.ptr); writefln(cast(int)b.ptr); // shouldn't b now point into an unallocated portion of memory? a.length=1; // pointers are still the same writefln(cast(int)a.ptr); writefln(cast(int)b.ptr); // this still works, though writefln(b[0]); fullCollect(); // even after a fullCollect(), this still works writefln(b[0]); } ---------------------------------------- I'm thinking that even when a is sliced and only a pointer into it is added, the new slice's range is added to the GC. This prevents an access violation when just this happens.
Jan 28 2005
prev sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"bobef" <bobef_member pathlink.com> wrote in message
news:ctdeg0$2gl9$1 digitaldaemon.com...
 I really need to write something like

 char[] sss;
 char &get(int i){return sss[i];}

 to get the "real" character... And I don't want ot involve pointers...
 Because as I undestand D policy, pointers are not "recommended", and goal
of D
 is to bring things to a higher level.
Is this a good time to resuggest reference return types? :-) inout char get(int i){return sss[i];} :-) <sinp>
Jan 28 2005