GameZelda
September 12th, 2009, 03:57 PM
Is it guaranted to work a conversion of a value type (such as int) to a byte pointer under any implementation of C#?
Like...
byte *bp = (byte *)integer;
Like...
byte *bp = (byte *)integer;
|
Click to See Complete Forum and Search --> : Value type to byte * GameZelda September 12th, 2009, 03:57 PM Is it guaranted to work a conversion of a value type (such as int) to a byte pointer under any implementation of C#? Like... byte *bp = (byte *)integer; BigEd781 September 12th, 2009, 05:20 PM 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: int i = 10; byte* b = (byte*) &i; However, this is obviously a bad idea because you have a potential for a loss of data. GameZelda September 13th, 2009, 04:37 AM 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: 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? BigEd781 September 13th, 2009, 06:24 AM C# is standardized, so yes, it would. I don't know of any other C# compilers though... codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |