Govilnag
Posts: 6
Joined: Mon Aug 24, 2020 12:30 pm

Pi variable memory address

Mon Nov 23, 2020 9:12 am

Hallo everyone,

i have created a variable for a sensor read and i want to know the "memory adress" of this variable wihch is constant.
i have tried id() but the varibale id number always change for a new reboot.is there any way to find the constant ram memory address of a python varialbe?

Heater
Posts: 17118
Joined: Tue Jul 17, 2012 3:02 pm

Re: Pi variable memory address

Mon Nov 23, 2020 10:21 am

Govilnag wrote:
Mon Nov 23, 2020 9:12 am
Hallo everyone,

i have created a variable for a sensor read and i want to know the "memory adress" of this variable wihch is constant.
i have tried id() but the varibale id number always change for a new reboot.is there any way to find the constant ram memory address of a python varialbe?
No. And you almost certainly don't need to. Variables in Python do not have fixed locations in memory.

It would help others help you if you would give in the most basic information about what you are trying to do and what you have tried. For example:

What is this sensor you are using?

How are you interfacing with that sensor, I2C, SPI, GPIO, UART, USB, other?

It also helps to post a minimal example of the code you are using that demonstrates the problem.
Memory in C++ is a leaky abstraction .

Govilnag
Posts: 6
Joined: Mon Aug 24, 2020 12:30 pm

Re: Pi variable memory address

Mon Nov 23, 2020 3:10 pm

thank you for the answer.

i am using a humidity snsor DHT11(digital) where i connected it to pi zero and also i have a temperature sensor(analog) with anoher pi zero via I2C connection. now i am using TCP/IP protocol from my master to get sensor value and display them on a dektop application.

Here instead of calling the sensor values by name i would like to tell my pi zero to get value of a variable (ex:temp or humidity) by giving it's RAM memory address (mostly hexadecimal value) like a pointer given for the ram address in ''C' programming. So, i am excpecting a RAM address of memory location of my variable which is constant in my processsor irrespective of sensor value changed.


i have already used id(hex(temp)), id(hex(humidity)) to get the memory address of my variables but every time i complie my code i am getting a new id values.

Heater
Posts: 17118
Joined: Tue Jul 17, 2012 3:02 pm

Re: Pi variable memory address

Mon Nov 23, 2020 3:22 pm

I cannot fathom any possible reason one would want to do anything like that.

The must be millions of examples of people reading sensors like that and forwarding data over network connections (using whatever protocol) and never needing to know an absolute memory address of anything. I cannot see anything in your project description that would warrant such a thing.

About the only time I use such absolute addresses it's when writing a Linux kernel driver or memory mapping device hardware into user memory space, when the addresses of the actual device hardware registers are needed.

In C we often use pointers (memory addresses) to access strings, structs, and so on, but it's almost never required to know what those addresses are.

Python does not have, or need, such pointers.

Can you post an example of your code that shows the need for actual addresses?

Edit: On reading again, you say: "instead of calling the sensor values by name i would like to tell my pi zero to get value of a variable (ex:temp or humidity) by giving it's RAM memory address"

Well my question is: Why do you want to do that?

If you mean that those addresses should be used over your TCP/IP connection protocol to specify which sensor to read that is just a bad way to design things. As you are discovering.
Memory in C++ is a leaky abstraction .

hippy
Posts: 8886
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Pi variable memory address

Mon Nov 23, 2020 4:14 pm

Govilnag wrote:
Mon Nov 23, 2020 3:10 pm
Here instead of calling the sensor values by name i would like to tell my pi zero to get value of a variable (ex:temp or humidity) by giving it's RAM memory address (mostly hexadecimal value) like a pointer given for the ram address in ''C' programming.
That is a flawed concept because the RAM address isn't guaranteed to remain constant.

I can't see the problem with using a name but you could use a pre-defined number rather than name, have the Zero receiving that number return the data requested according to number received.

The overhead in using a pre-defined number, probably even a name, is so minimal that it's not worth trying to optimise in the way you you are thinking of doing that.

Return to “General discussion”