CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2007
    Posts
    69

    Value type to byte *

    Is it guaranted to work a conversion of a value type (such as int) to a byte pointer under any implementation of C#?

    Like...
    Code:
    byte *bp = (byte *)integer;

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Value type to byte *

    That code would never work because you are casting an int to a byte pointer (well, it may appear to 'work' depending on the value of 'integer' and what lies at that address in memory. It would most likely crash with a Null Reference exception when you tried to take the value at b). You code will set the pointer value itself to whatever the value of 'integer' is. For example, if 'integer' was '10'. your pointer would point to address '10', it would not point to a value of '10. It would have to be like this:

    Code:
    int i = 10;
    byte* b = (byte*) &i;
    However, this is obviously a bad idea because you have a potential for a loss of data.
    Last edited by BigEd781; September 12th, 2009 at 05:22 PM.

  3. #3
    Join Date
    Dec 2007
    Posts
    69

    Re: Value type to byte *

    Quote Originally Posted by BigEd781 View Post
    That code would never work because you are casting an int to a byte pointer (well, it may appear to 'work' depending on the value of 'integer' and what lies at that address in memory. It would most likely crash with a Null Reference exception when you tried to take the value at b). You code will set the pointer value itself to whatever the value of 'integer' is. For example, if 'integer' was '10'. your pointer would point to address '10', it would not point to a value of '10. It would have to be like this:

    Code:
    int i = 10;
    byte* b = (byte*) &i;
    However, this is obviously a bad idea because you have a potential for a loss of data.
    Oops, I forgot the "&" symbol.
    Is the code like the one that you posted supposed to work under any implementation?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Value type to byte *

    C# is standardized, so yes, it would. I don't know of any other C# compilers though...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured