Created
May 20, 2017 14:58
-
-
Save ohtaman/a47cc5f3c20356ce7d37dd48bd77efc9 to your computer and use it in GitHub Desktop.
メモリのアドレスからnp.arrayを生成
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ctypes import c_char_p, c_byte, cast, ARRAY, POINTER | |
| import numpy as np | |
| a = np.array(list(range(10)), dtype=np.uint8) | |
| a_addr = a.__array_interface__['data'][0] | |
| size = len(a) | |
| a_char_p = c_char_p(a_addr) | |
| a_byte_array = cast(a_char_p, POINTER(ARRAY(c_byte, len=size))).contents | |
| b = np.frombuffer(a_byte_array, dtype=np.uint8) | |
| print(a) | |
| print(b) | |
| a[1] = 3 | |
| print(a) | |
| print(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment