Is it possible to get the Physical memory address of a variable inpython?

O

Ognjen Bezanov

Hello all,

Say I have a python variable:

a = "hello"

Is it possible for me to get the physical address of that variable (i.e.
where it is in RAM)?

I know that id(a) will give me it's memory address, but the address
given does not seem to correlate with the physical memory. Is this even
possible?


Thank you!


Ognjen
 
C

Carl Banks

Hello all,

Say I have a python variable:

a = "hello"

Is it possible for me to get the physical address of that variable (i.e.
where it is in RAM)?

I know that id(a) will give me it's memory address, but the address
given does not seem to correlate with the physical memory. Is this even
possible?


I'm going to guess that

A. You don't really want physical address but logical address (i.e.,
the address where you might access the memory with a C pointer), and

B. You want the address not of the object itself, but of the data
within (that is, you'd want the "pointer" you receive to point to the
ASCII string hello)

Python's way to handle cases like this is called buffer protocol, but
it only operates at the C-extension level. There is a set of
functions in the C API that look like this

PyObject_AsReadBuffer(obj,**buffer,*len)

Calling this will return the address of the string "hello" in
*buffer. See C API Reference Manual for more details. This function
can also be called from ctypes, but I don't remember the exact
procedure for modifying input arguments through a pointer.

Note: If you really want a PHYSCIAL RAM address you'll have to make
some OS-specific system call (I think) with the logical address,
probably needing superuser privileges. Obviously this call isn't
exposed in Python, because there's no use for it in Python. Unless
you're programming DMA transfers or something like that, there's no
use for it in C, either.


Carl Banks
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top