from ctypes import string_at
from sys import getsizeof
from binascii import hexlify
secret = np.array([0xabababab, 0xbcbcbcbc, 0xcdcdcdcd])
print("secret : ", hex(id(secret)))
print(hexlify(string_at(id(secret), getsizeof(secret))))
print("secret.data : ", secret.data, hex(id(secret.data)))
print(hexlify(string_at(id(secret.data), getsizeof(secret.data))))
print("data[0] : ", secret[0], hex(id(secret[0])))
print(hexlify(string_at(id(secret[0]), getsizeof(secret[0]))))
print("data[1] : ", secret[1], hex(id(secret[1])))
print(hexlify(string_at(id(secret[1]), getsizeof(secret[1]))))
print("data[2] : ", secret[2], hex(id(secret[2])))
print(hexlify(string_at(id(secret[2]), getsizeof(secret[2]))))
I am analyzing the memory of numpy's ndarray.
While checking the memory address, I noticed an incomprehensible output.
We saw that the memory addresses of secret[0], secret[1], and secret[2] are all the same, but the stored values are different.
How is this possible?
Isn't the id() function just returning a memory address?
I will wait for the answers from the masters. Thank you.
ps: If you have any books or resources that deal with Python's memory structure, please recommend it. :)
secret : 0x7f8e61d6b990
b'0100000000000000201ff95f8e7f00009058d000000000000100000000000000608805010000000068880501000000000000000000000000a037f95f8e7f00000705000000000000000000000000000001000000000000004084a300000000001d0000000000000013e3adb2fffc9b90e4686f6d652f6861'
secret.data : <memory at 0x7f8e5cd89708> 0x7f8e5a17c348
b'0000000000000000a09ca3000000000003000000000000000000000000000000ffffffffffffffff070000006e6f206100000000000000009058d0000000000090b9d6618e7f0000180000000000000008000000000000000000000001000000f0341c01000000009897d85c8e7f0000a097d85c8e7f000000000000000000000000000000000000000000000000000003000000000000000800000000000000000053000000530002000000000000004084a300000000008d00000000000000'
data[0] : 2880154539 0x7f8e5a332240 //same address!
b'4022335a8e7f0000a08cfa5f8e7f0000abababab000000005822335a8e7f0000' //0xababab
data[1] : 3166485692 0x7f8e5a332240 //same address!
b'4022335a8e7f0000a08cfa5f8e7f0000bcbcbcbc000000005822335a8e7f0000' //0xbcbcbc
data[2] : 3452816845 0x7f8e5a332240 //same address!
b'4022335a8e7f0000a08cfa5f8e7f0000cdcdcdcd000000005822335a8e7f0000' //0xcdcdcd