|
-
September 12th, 2009, 03:57 PM
#1
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;
-
September 12th, 2009, 05:20 PM
#2
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.
-
September 13th, 2009, 04:37 AM
#3
Re: Value type to byte *
 Originally Posted by BigEd781
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?
-
September 13th, 2009, 06:24 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|