digitalmars.D - function return value in or out?
- bobef (6/6) Jan 27 2005 Consider following example:
- Derek Parnell (9/17) Jan 27 2005 It returns a copy of the value in the array. If you modify the returned
- bobef (12/29) Jan 28 2005 I really need to write something like
- Derek (24/26) Jan 28 2005 Does this help ...
- bobef (2/28) Jan 28 2005
- Sebastian Beschke (9/11) Jan 28 2005 I guess in this case, pointers are pretty much your only viable option.
- Jarrett Billingsley (8/11) Jan 28 2005 That still, unfortunately, doesn't allow you to edit the character in th...
- brad beveridge (5/23) Jan 28 2005 Isn't there the fundamental problem of array relocation though? What
- Jarrett Billingsley (31/34) Jan 28 2005 I tried what you said, and this snippet shows it..
- Ivan Senji (8/14) Jan 28 2005 of D
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
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
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
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
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
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
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
Jarrett Billingsley wrote: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. Bradvoid 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
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
"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 goalof Dis 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