Connecting Tech Pros Worldwide Forums | Help | Site Map

pointers, positions and datastorage

Mark Broadbent
Guest
 
Posts: n/a
#1: Nov 15 '05
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 . . .



Morten Wennevik
Guest
 
Posts: n/a
#2: Nov 15 '05

re: pointers, positions and datastorage


On Thu, 4 Mar 2004 14:54:01 -0000, Mark Broadbent
<no-spam-please@no-spam-please.com> wrote:
[color=blue]
> 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?
>[/color]
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.
[color=blue]
> 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,[/color]

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/
Mark Broadbent
Guest
 
Posts: n/a
#3: Nov 15 '05

re: pointers, positions and datastorage


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" <MortenWennevik@hotmail.com> wrote in message
news:opr4cf2yq0hntkfz@msnews.microsoft.com...[color=blue]
> On Thu, 4 Mar 2004 14:54:01 -0000, Mark Broadbent
> <no-spam-please@no-spam-please.com> wrote:
>[color=green]
> > 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?
> >[/color]
> 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.
>[color=green]
> > 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[/color][/color]
to[color=blue][color=green]
> > 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,[/color]
>
> 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/[/color]


Daniel Jin
Guest
 
Posts: n/a
#4: Nov 15 '05

re: pointers, positions and datastorage



----- Mark Broadbent wrote: ----
[color=blue]
> 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[/color]

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
[color=blue]
> 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[/color]

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" <MortenWennevik@hotmail.com> wrote in messag
news:opr4cf2yq0hntkfz@msnews.microsoft.com..[color=blue]
> On Thu, 4 Mar 2004 14:54:01 -0000, Mark Broadben
><no-spam-please@no-spam-please.com> wrote[color=green][color=darkred]
>>> hi all. Ive been looking into the use of pointers to try and get my hea[/color]
>> 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[color=darkred]
>>> You don't, and the garbage collector will change the positions too. Eac[/color][/color]
> 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[color=green]
>> But since C# also don't use pointers to objects, but references, you don'[/color]
> 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[color=green][color=darkred]
>>> 2. Where can I find out how the physical binary (or byte[/color]
>> 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[/color][/color]
t[color=blue][color=green]
>> 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!)[color=darkred]
>>>> hope this all made sense,[/color]
>> What exactly are you trying to achieve? One of the major benefits of C#[/color]
> 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.[color=green][color=darkred]
>>> --[/color][/color]
> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/[/color]



Mattias Sjögren
Guest
 
Posts: n/a
#5: Nov 15 '05

re: pointers, positions and datastorage


Mark,
[color=blue]
>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)]?[/color]

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.

[color=blue]
>an int pointer (which increments by byte)[/color]

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



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mattias Sjögren
Guest
 
Posts: n/a
#6: Nov 15 '05

re: pointers, positions and datastorage


Morten,
[color=blue]
>You don't, and the garbage collector will change the positions too.[/color]

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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mark Broadbent
Guest
 
Posts: n/a
#7: Nov 15 '05

re: pointers, positions and datastorage


Thankyou all .. Mattias, Daniel and Morten for your input.

--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
"Mark Broadbent" <no-spam-please@no-spam-please.com> wrote in message
news:%23Vq0JifAEHA.1700@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
reverse[color=blue]
> order of declaration (see code below). Is this always the case -so that[/color]
you[color=blue]
> can guarantee which variable you are effectively hitting?
>
> 2. Where can I find out how the physical binary (or byte) representation[/color]
of[color=blue]
> numbers signed and unsigned(and other types) Please give me a good text[/color]
ref[color=blue]
> that describes it such as a url)]? One reason being is because I might[/color]
have[color=blue]
> 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[/color]
I[color=blue]
> 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 . . .
>
>[/color]


Closed Thread