www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - delegate memory layout help needed

reply nobody <nobody mailinator.com> writes:
I am trying to debug some code that behaves strangely around delegates. I have 
been assuming that delegates are just structs with two int-sized fields (just 
like arrays). Is this correct? Is this struct defined anywhere in DMD supplied
D 
code?

More importantly I have been trying to watch two delegate members of a struct
to 
  make sure they are getting initialized:

     printf("&opIndexDg : [%X]\n", opIndexDg );
     printf("    +0 -> [%X]\n", *(cast(int*)(&opIndexDg)+0) );
     printf("    +1 -> [%X]\n", *(cast(int*)(&opIndexDg)+1) );

     printf("&opIndexAssignDg : [%X]\n", &opIndexAssignDg );
     printf("     +0 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+0) );
     printf("     +1 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+1) );

According to what I have seen the delegates in the struct are getting 
initialized but I keep getting an Array Bounds Error unless I do the exact same 
assignment done during the initialization afterwards in main(). Am I correctly 
interpreting the memory layout?
Feb 13 2007
parent reply torhu <fake address.dude> writes:
nobody wrote:
 More importantly I have been trying to watch two delegate members of a struct
to 
   make sure they are getting initialized:
 
      printf("&opIndexDg : [%X]\n", opIndexDg );
      printf("    +0 -> [%X]\n", *(cast(int*)(&opIndexDg)+0) );
      printf("    +1 -> [%X]\n", *(cast(int*)(&opIndexDg)+1) );
 
      printf("&opIndexAssignDg : [%X]\n", &opIndexAssignDg );
      printf("     +0 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+0) );
      printf("     +1 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+1) );
 
 According to what I have seen the delegates in the struct are getting 
 initialized but I keep getting an Array Bounds Error unless I do the exact
same 
 assignment done during the initialization afterwards in main(). Am I correctly 
 interpreting the memory layout?
You can access the context pointer with .ptr, and the function pointer with .funcptr. See: http://www.digitalmars.com/d/function.html#closures As for the array bounds error, it's hard to tell without some more context. A common mistake is to return a delegate whose .ptr points to stack data.
Feb 13 2007
parent nobody <nobody mailinator.com> writes:
torhu wrote:
 nobody wrote:
 More importantly I have been trying to watch two delegate members of a 
 struct to   make sure they are getting initialized:

      printf("&opIndexDg : [%X]\n", opIndexDg );
      printf("    +0 -> [%X]\n", *(cast(int*)(&opIndexDg)+0) );
      printf("    +1 -> [%X]\n", *(cast(int*)(&opIndexDg)+1) );

      printf("&opIndexAssignDg : [%X]\n", &opIndexAssignDg );
      printf("     +0 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+0) );
      printf("     +1 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+1) );

 According to what I have seen the delegates in the struct are getting 
 initialized but I keep getting an Array Bounds Error unless I do the 
 exact same assignment done during the initialization afterwards in 
 main(). Am I correctly interpreting the memory layout?
You can access the context pointer with .ptr, and the function pointer with .funcptr. See: http://www.digitalmars.com/d/function.html#closures
Thanks for the suggestion. I realized when I tried to reference .ptr and .funcptr and the compiler complained that these properties did not exist that I was still using an older version of DMD. When I upgraded the strange bug disappeared!
Feb 14 2007