473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointers, positions and datastorage

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 . . .
Nov 15 '05 #1
6 1266
On Thu, 4 Mar 2004 14:54:01 -0000, Mark Broadbent
<no************@no-spam-please.com> wrote:
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.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
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" <Mo************@hotmail.com> wrote in message
news:op**************@msnews.microsoft.com...
On Thu, 4 Mar 2004 14:54:01 -0000, Mark Broadbent
<no************@no-spam-please.com> wrote:
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.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Nov 15 '05 #3

----- 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" <Mo************@hotmail.com> wrote in messag
news:op**************@msnews.microsoft.com.. On Thu, 4 Mar 2004 14:54:01 -0000, Mark Broadben
<no************@no-spam-please.com> wrote
hi all. Ive been looking into the use of pointers to try and get my hea

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
You don't, and the garbage collector will change the positions too. Eac 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/


Nov 15 '05 #4
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #5
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #6
Thankyou all .. Mattias, Daniel and Morten for your input.

--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 . . .

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
42
by: Abhishek Jha | last post by:
We cann't add two pointers, We cann't multiply two pointers, We cann't divide two pointers, But We can subtract two pointers. Why ?
4
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array...
0
by: KK | last post by:
Dear All I have an MDI Application which needs to restore it's previously closed state on subsequent openings. While closing the application, I am storing the positions of all the Mdi child...
4
by: Anon | last post by:
Hello All! I have a long string that I need to make sense out of. I have the documentation about what info is between which characters, I just need to somehow parse each 94 character string into...
9
by: mazzawi | last post by:
is it possible to have pointers to bit fields? i read somewhere that you can't. but what if we do this typedef struct u_int3_t { unsigned int u_int3_t:3; } CMI; u_int3_t Array;
96
by: subramanian | last post by:
I have been thinking that all pointers(to any obejct) have the same size. The size of a pointer is the size of an int. This is beause a memory location is addressed by an int. Is that right/wrong?
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
11
by: krreks | last post by:
Hi. I'm trying to figure out pointers and memory allocation in C. I've read quite a few explanations on the internet and have read in a few books that I've got, but I'm not so much smarter yet......
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.