pointers, positions and datastorage

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 . . .
 
M

Morten Wennevik

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?
You don't, and the garbage collector will change the positions too. Each
new allocation will be put ontop of everything else, causing memory
allocation to be faster than in C/C++. But the garbage collector will
change the position when it releases objects that have been allocated
earlier so that there are no free memory "holes". There is no way of
knowing (as far as I know) "where" an object is at any given moment.
Assuming you aren't using unsafe.

But since C# also don't use pointers to objects, but references, you don't
worry about it. The reference table will be changed by the garbage
collector when needed, but the reference will always point to the correct
object even if it may move around inside memory.
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,

What exactly are you trying to achieve? One of the major benefits of C#
is that it doesn't use pointers.
You can still use them in an unsafe codeblock in C# though. Can't help
you with the sizes though.
 
M

Mark Broadbent

The purpose for all my questions stems from the fact that I am trying to
comprehend what the hell is the point of being able to increment/increase or
decrement/decrease (via ++ -- +num -num) a pointer so that it points to
another element/memory location IF you cant positionally know what the next
variable element/s are going to be. I therefore believe (although you
suggest that this is not the case) that variables (by declaration) are
stored in memory in reverse order (next to each other) for the duration of
their lifecycle.

The reason that knowing the physical structure of data types is important
with relation to pointers is that you may want to use a int* pointer (and
increment/decrement it) to modify a whole range of memory locations of
different types -without causing a program exception because you have just
set an invalid value to the second byte of a double.

--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
Morten Wennevik said:
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?
You don't, and the garbage collector will change the positions too. Each
new allocation will be put ontop of everything else, causing memory
allocation to be faster than in C/C++. But the garbage collector will
change the position when it releases objects that have been allocated
earlier so that there are no free memory "holes". There is no way of
knowing (as far as I know) "where" an object is at any given moment.
Assuming you aren't using unsafe.

But since C# also don't use pointers to objects, but references, you don't
worry about it. The reference table will be changed by the garbage
collector when needed, but the reference will always point to the correct
object even if it may move around inside memory.
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,

What exactly are you trying to achieve? One of the major benefits of C#
is that it doesn't use pointers.
You can still use them in an unsafe codeblock in C# though. Can't help
you with the sizes though.
 
G

Guest

----- Mark Broadbent wrote: ----
The purpose for all my questions stems from the fact that I am trying t
comprehend what the hell is the point of being able to increment/increase o
decrement/decrease (via ++ -- +num -num) a pointer so that it points t
another element/memory location IF you cant positionally know what the nex
variable element/s are going to be. I therefore believe (although yo
suggest that this is not the case) that variables (by declaration) ar
stored in memory in reverse order (next to each other) for the duration o
their lifecycle

I don't think (could be wrong) that there is a reliable pattern to how variables are laid out in memory relative to each other. if fact, if you add more variables to your code, you will see there is no clear pattern. increment/decrement is typically used with array of a value type, where elements of the array are laid out in sequential block, and you know the size of each element. so you know the boundary of the array and can safely walk it. Is there a reason why you would need to use the pointer to one variable as a relative jump location to other variables in memory
The reason that knowing the physical structure of data types is importan
with relation to pointers is that you may want to use a int* pointer (an
increment/decrement it) to modify a whole range of memory locations o
different types -without causing a program exception because you have jus
set an invalid value to the second byte of a double

why? what advantage would that have over modifying a double as a double (besides the fact that it's cool)? but I'm sure that information is out there, I just never had to do it

--
Br
Mark Broadben
mcdba,mcse+
======================
Morten Wennevik said:
On Thu, 4 Mar 2004 14:54:01 -0000, Mark Broadben
around them which has led me onto a question
1. How do you know what memory position C# compiler will use for eac
variable declaration as this example shows they seem to be stored i
reverse order of declaration (see code below). Is this always the cas
-so that you can guarantee which variable you are effectively hitting
new allocation will be put ontop of everything else, causing memor
allocation to be faster than in C/C++. But the garbage collector wil
change the position when it releases objects that have been allocate
earlier so that there are no free memory "holes". There is no way o
knowing (as far as I know) "where" an object is at any given moment
Assuming you aren't using unsafe
But since C# also don't use pointers to objects, but references, you don'
worry about it. The reference table will be changed by the garbag
collector when needed, but the reference will always point to the correc
object even if it may move around inside memory
2. Where can I find out how the physical binary (or byte
representation o
numbers signed and unsigned(and other types) Please give me a good tex
re
that describes it such as a url)]? One reason being is because I migh
hav
an int pointer (which increments by byte) and increment it so it point
t
the first byte of a double -to make a change to this byte I would nee t
know how the double is physically represented across both bytes
otherwise
would not be able to determine the resulting outcome of the doubl
itself
Another reason I found was to accurately predict the result of bitwis
shifting of numbers (i.e. what i would expect and what i got!)
hope this all made sense,
What exactly are you trying to achieve? One of the major benefits of C#
is that it doesn't use pointers.
You can still use them in an unsafe codeblock in C# though. Can't help
you with the sizes though.Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
 
M

Mattias Sjögren

Mark,
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)]?

Single and double complies with the IEEE 754 standard. A Google search
for that should give you plenty of hits with details on the format.

sbyte/short/int/long are standard 8/16/32/64 twos-compliment integers.
On Intel machines they are in little endian order (LSB at lowest
address), but that's implementation specific.

an int pointer (which increments by byte)

An int* increments in steps of 4 bytes (sizeof(int)).



Mattias
 
M

Mattias Sjögren

Morten,
You don't, and the garbage collector will change the positions too.

The GC will not move local, stack allocated variables such as i, j and
k in Mark's code.

But the runtime may of course lay out the variables on the stack in
whatever order it wants.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top