fbpx
Welcome, Guest
Username: Password: Remember me
Welcome to the XSharp forum!

Tell us and our members who you are, what you like and why you became a member of this site.
We welcome all new members and hope to see you around a lot!
  • Page:
  • 1

TOPIC:

Casting, dereferencing & C. 03 Jun 2021 14:55 #18716

  • antonello.negrone


  • Posts: 12
  • The following test lines of code in VO always return 5, instead in X# dont' work as expected:
    FUNCTION Start( ) AS VOID
    	LOCAL DIM p[4]	AS BYTE 
    
    	p[1]	:=	5
    	System.Console.WriteLine( AsString( DWORD(@p[1])))    //strange results
    	System.Console.WriteLine( Bin2DW(Mem2String( @p[1],4)) )   //correct result
    RETURN

    The output of "strange results" line is alway different at each run, the second is always correct.
    The test app is compiled in VO dialect with /unsafe + /vo7

    TIA for the help
    Antonello

    Please Log in or Create an account to join the conversation.

    Casting, dereferencing & C. 03 Jun 2021 15:55 #18717

    • robert
    • robert's Avatar


  • Posts: 2222
  • Antonello,

    You are probably expecting that
    DWORD(@p[1])

    will take the address of @P[1] and treat that like a DWORD PTR and you want to dereference that pointer and get the DWORD value at that location.

    However the DWORD( something ) code in VO can mean 2 things:

    - derefence a pointer
    - convert the value to a DWORD

    X# is doing the second in this case and therefore gives you the address as a DWORD. Each time you run the app that address is different.

    I would recommend to do something like this instead (untested)
    LOCAL dwptr as DWORD PTR
    dwptr := @p[1]    // you can probably also do dwptr := @p
    System.Console.WriteLine( AsString( dwptr[1]))

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Casting, dereferencing & C. 03 Jun 2021 17:10 #18718

    • leon-ts


  • Posts: 137
  • Antonello,

    You just need to "tell" the compiler that in this context you mean a pointer, not a pointer value. To do this, it is enough to cast to the PTR type:
    System.Console.WriteLine( AsString( DWORD( (PTR)@p[1] ) ) )

    Best regards,
    Leonid

    Please Log in or Create an account to join the conversation.

    Casting, dereferencing & C. 03 Jun 2021 17:36 #18719

    • antonello.negrone


  • Posts: 12
  • Robert & Leonid, thank you, that does the trick!

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1