M
Mark Broadbent
hi all. Ive been looking into the use of pointers to try and get my head
around them which has led me onto a question.
1. How do you know what memory position C# compiler will use for each
variable declaration as this example shows they seem to be stored in reverse
order of declaration (see code below). Is this always the case -so that you
can guarantee which variable you are effectively hitting?
2. Where can I find out how the physical binary (or byte) representation of
numbers signed and unsigned(and other types) Please give me a good text ref
that describes it such as a url)]? One reason being is because I might have
an int pointer (which increments by byte) and increment it so it points to
the first byte of a double -to make a change to this byte I would need to
know how the double is physically represented across both bytes, otherwise I
would not be able to determine the resulting outcome of the double itself.
Another reason I found was to accurately predict the result of bitwise
shifting of numbers (i.e. what i would expect and what i got!).
hope this all made sense,
--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
using System;
class PtrArithDemo {
unsafe public static void Show(string name,int* ptr) {
Console.WriteLine(name + "(int)\tpos = " + (uint) ptr + "\tvalue = " +
*ptr);
}
unsafe public static void Show(string name,double* ptr) {
Console.WriteLine(name + "(dbl)\tpos = " + (uint) ptr + "\tvalue = " +
*ptr);
}
unsafe public static void Main() {
int i;
double j;
int k;
int* ip = &i;
double* jp = &j;
int* kp = &k;
//set initial values to variables
i=1;j=2;k=3;
//show current positions and values
Show("ip",ip); Show("jp",jp); Show("kp",kp);
}
}
gives output of...
ip(int) pos = 1242788 value = 1
jp(dbl) pos = 1242780 value = 2
kp(int) pos = 1242776 value = 3
Press any key to continue . . .
around them which has led me onto a question.
1. How do you know what memory position C# compiler will use for each
variable declaration as this example shows they seem to be stored in reverse
order of declaration (see code below). Is this always the case -so that you
can guarantee which variable you are effectively hitting?
2. Where can I find out how the physical binary (or byte) representation of
numbers signed and unsigned(and other types) Please give me a good text ref
that describes it such as a url)]? One reason being is because I might have
an int pointer (which increments by byte) and increment it so it points to
the first byte of a double -to make a change to this byte I would need to
know how the double is physically represented across both bytes, otherwise I
would not be able to determine the resulting outcome of the double itself.
Another reason I found was to accurately predict the result of bitwise
shifting of numbers (i.e. what i would expect and what i got!).
hope this all made sense,
--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
using System;
class PtrArithDemo {
unsafe public static void Show(string name,int* ptr) {
Console.WriteLine(name + "(int)\tpos = " + (uint) ptr + "\tvalue = " +
*ptr);
}
unsafe public static void Show(string name,double* ptr) {
Console.WriteLine(name + "(dbl)\tpos = " + (uint) ptr + "\tvalue = " +
*ptr);
}
unsafe public static void Main() {
int i;
double j;
int k;
int* ip = &i;
double* jp = &j;
int* kp = &k;
//set initial values to variables
i=1;j=2;k=3;
//show current positions and values
Show("ip",ip); Show("jp",jp); Show("kp",kp);
}
}
gives output of...
ip(int) pos = 1242788 value = 1
jp(dbl) pos = 1242780 value = 2
kp(int) pos = 1242776 value = 3
Press any key to continue . . .