473,320 Members | 2,029 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,320 software developers and data experts.

Getting stackpointer safely

Is there a way to get an approximate stackpointer into an int in C#?
The following works, but is unsafe and adds a new variable to the
stackframe:

int i = (int)(&i);

It seems like it should be possible to do this safely because I never
dereference a pointer. Perhaps there is a safe way to load a register
value into an int?
Nov 16 '05 #1
3 1892
You'll need to use unsafe code to get a pointer.

What would you do with a pointer if you never dereferenced it? Remember
that
the CLR is free to move your objects around the heap. And while a value
type
may be on the stack, a value type that is a member of a reference type will
not be.

--
-Philip Rieck
http://philiprieck.com/blog/

-
"Mike Spertus" <Mi**********@veritas.com> wrote in message
news:4b**************************@posting.google.c om...
Is there a way to get an approximate stackpointer into an int in C#?
The following works, but is unsafe and adds a new variable to the
stackframe:

int i = (int)(&i);

It seems like it should be possible to do this safely because I never
dereference a pointer. Perhaps there is a safe way to load a register
value into an int?

Nov 16 '05 #2
"Philip Rieck" <ju**@mckraken.com> wrote in message news:<#g**************@TK2MSFTNGP12.phx.gbl>...
You'll need to use unsafe code to get a pointer.

What would you do with a pointer if you never dereferenced it? Remember
that
the CLR is free to move your objects around the heap. And while a value
type
may be on the stack, a value type that is a member of a reference type will
not be.

--
-Philip Rieck
http://philiprieck.com/blog/

-

Unfortunately, I can't discuss the reason I need it here, but let me
just say that it is a good one that does not ever involve
dereferencing a pointer. Certainly one should be able to think of many
reasons why one might want to know what the stackpointer is. Since I
am only interested in the stackpointer, I'm not really concered
whether objects move around the heap.

In any case, there's nothing inherently unsafe about putting the ESP
register in an int as long as I don't cast it to a pointer, which
would indeed be recognized as unsafe. As far as I can tell, there's no
intrinsic reason one couldn't safely put a register (or the
stackpointer) into an int. However, I haven't found a way to do it.
Nov 16 '05 #3

"Mike Spertus" <Mi**********@veritas.com> wrote in message
news:4b**************************@posting.google.c om...
"Philip Rieck" <ju**@mckraken.com> wrote in message
news:<#g**************@TK2MSFTNGP12.phx.gbl>...
You'll need to use unsafe code to get a pointer.

What would you do with a pointer if you never dereferenced it? Remember
that
the CLR is free to move your objects around the heap. And while a value
type
may be on the stack, a value type that is a member of a reference type
will
not be.

--
-Philip Rieck
http://philiprieck.com/blog/

-

Unfortunately, I can't discuss the reason I need it here, but let me
just say that it is a good one that does not ever involve
dereferencing a pointer. Certainly one should be able to think of many
reasons why one might want to know what the stackpointer is. Since I
am only interested in the stackpointer, I'm not really concered
whether objects move around the heap.

In any case, there's nothing inherently unsafe about putting the ESP
register in an int as long as I don't cast it to a pointer, which
would indeed be recognized as unsafe. As far as I can tell, there's no
intrinsic reason one couldn't safely put a register (or the
stackpointer) into an int. However, I haven't found a way to do it.


The compiler will require unsafe to keep you from playing any cute tricks,
like getting the pointer of a local, casting it to int and sending it off to
a method that *does* dereference the pointer value. It is just not
effectivly safe to do.

Now, more to the point, within .NET itself, ESP doesn't exist. It has an
instruction stack and no registers and does not inherently have the concept
of a register. Any assumptions you make as to which variable happens to
match up with the beginning of the stack, or even that ESP points to the
same effective stack that the variable exists on is potentially dangerous.
The stack is abstracted and not guarenteed to exist in any particular manner
or order after the JIT and compiler's run.

I do not think there is any reliable way to get a stack pointer within the
language itself, that seems to be relegated to debuggers and the like.
Unsafe code and accessing the first variable is as close as you are going to
come.
Nov 16 '05 #4

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

Similar topics

4
by: jt | last post by:
I'm getting a compiler error: warning C4172: returning address of local variable or temporary Here is the function that I have giving this error: I'm returning a temporary char string and its...
12
by: prism via DotNetMonster.com | last post by:
question: how do we safely remove a USB device in vb.net? for example, in the vb.net application, we open some files from a usb device, and after that, we want to "safely remove" it without...
4
by: tech | last post by:
Hi, I need to pass a block of say 320 bytes memory between some classes which do various processing on it. The app needs to be quick so i can't keep copying. The simplest way is via pointer...
4
AmberJain
by: AmberJain | last post by:
Hello, After a successful installation of Xubuntu, I am posting this question. In Windows XP we select option "Safely remove hardware" before we remove a USB drive (or Pen drive) from a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.