www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Exact effects of returning Structs

reply Era Scarecrow <rtcvb32 yahoo.com> writes:
  I have some experimental code I'm writing, and it seems when i return a
structure it is either corrupted, or should be disallowed all together. A
postblit is suggested this(this), but since I'm returning a structure that
isn't tied to anything else it doesn't need to duplicate it.

Example:

struct A{
int b;
char c[];
}

void fun1() {
  A a;
  a = fun2();
//array a.c corrupted, causes range exception
//int a.b corrupted?!?
}

A fun2(){
  A t;
  t.c.length = 100;
/*do something*/
  return t;  //valid before returning.
}



      
Aug 27 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Era Scarecrow:

   I have some experimental code I'm writing, and it seems when i return a
structure it is either corrupted, or should be disallowed all together. A
postblit is suggested this(this), but since I'm returning a structure that
isn't tied to anything else it doesn't need to duplicate it.

If possible please show a complete minimal program that contains a main() that shows your problem. I have written this, but I don't see the problem: import std.stdio: writeln, write; enum int N = 100; struct Foo { int x; char[] arr; } Foo fun2() { Foo f1; f1.arr.length = N; f1.arr[] = 'x'; f1.x = 25; writeln(f1.arr.length); write(">"); foreach(i; 0 .. N) write(f1.arr[i]); writeln("<"); return f1; } void main() { Foo f2; f2 = fun2(); writeln(f2.x); writeln(f2.arr.length); write(">"); foreach(i; 0 .. N) write(f2.arr[i]); writeln("<"); } Bye, bearophile
Aug 27 2010
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
== Quote from bearophile (bearophileHUGS lycos.com)'s article
 Era Scarecrow:
   I have some experimental code I'm writing, and it seems when i return a


is suggested this(this), but since I'm returning a structure that isn't tied to anything else it doesn't need to duplicate it.
 If possible please show a complete minimal program that contains a main() that

I know i've been going a different route with the code since so i am not sure where exactly it was, and can't duplicate the problem now. However single-lining through it brings up a question of assumption. Aren't array's (dynamic and otherwise) filled with 0's by default? Str_BigNum add(in Str_BigNum rhs) { Str_BigNum tmp; char res[]; res.length = _result.length + 1; res[1 .. $] = _result[]; //res[0] should be null, right?? _addsub!("+")(res, rhs._result); tmp._result = res; return tmp; }
Aug 27 2010
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article
 array's (dynamic and otherwise) filled with 0's by default?

-Steve

Ahh that's right. It's 0xFF which is a illegal character for UTF-8 encoding. That does help explain why my asserts involving 0 to 9 kept failing, since my checks were for 0x0, and 0x20. If i happen to duplicate the original problem i'll post back again.
Aug 27 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 27 Aug 2010 16:04:50 -0400, Era Scarecrow <rtcvb32 yahoo.com>  
wrote:

 == Quote from bearophile (bearophileHUGS lycos.com)'s article
 Era Scarecrow:
   I have some experimental code I'm writing, and it seems when i  


A postblit is suggested this(this), but since I'm returning a structure that isn't tied to anything else it doesn't need to duplicate it.
 If possible please show a complete minimal program that contains a  
 main() that

I know i've been going a different route with the code since so i am not sure where exactly it was, and can't duplicate the problem now. However single-lining through it brings up a question of assumption. Aren't array's (dynamic and otherwise) filled with 0's by default?

No, arrays are filled with the .init value. For char, that's 0xff. -Steve
Aug 27 2010