Filip Matyja
Filip Matyja

Reputation: 137

Memory Address - what is the use of it for programmer?

Evening community,

I'm learning Python and more then often I can see addresses of objects in the memory like:

... at 0x019F60F0

That python is sharing with me when I'm running programs. How useful it will be for me in the future, if anyhow? Or is it just a side-effect of running the program, and human has no use of it?

Upvotes: 1

Views: 125

Answers (2)

Ryan Sam
Ryan Sam

Reputation: 2997

To answer your question on are memory addresses useful, they defiantly are. I'm not a python developer so I don't know why you're getting addresses outputted to you but in general they can be used in lots of different ways.
For starters they can be very helpful in when debugging code. If you need to know exactly what the value is in a memory address at an exact point of time in your code you can set a break point and depending on what IDE you're using, like in Visual Studio 2017, you can look at the value in the memory address and compare it to what you were expecting.
Another useful thing is for optimization. In many programming languages instead of creating a new variable which takes up more space in memory you can reference to a memory address that already holds that value. In machines that have limited amounts of memory like embedded systems, or programs that need to be really fast, in the long run can have huge performance gains.
In my last example I'll talk about security. Although I'm not an expert on this topic I do know that by looking through parts of memory in certain systems, like servers, you can see if you're/you have been attacked. You can tell by looking for patterns of corrupted memory. I'm sure there are many attacks that corrupt your memory but one I can think of is a buffer overflow. Buffer Overflow

Upvotes: 1

cat
cat

Reputation: 4020

juanpa.arrivillaga said:

This is merely the default repr that all types inherit from object. The main use is for debugging and being able to distinguish distinct objects, and the address itself is the equivalent of calling hex(id(some_object)) in CPython (although that is an implementation detail).

Upvotes: 1

Related Questions