ctypes.string_at(address, size=-1) reads raw memory at a given address and returns a copy of it as a Python bytes object. If you leave out size, it reads up to the first zero byte, which is how a C string is terminated. Use it when a C library hands you a char * or a buffer address and you need the contents as Python data.
The function lives in the standard library ctypes module, which is available on every major platform. Because it trusts the address you give it, a wrong pointer can crash the interpreter, so it needs more care than normal Python code.
| Parameter | Meaning |
|---|---|
address | A ctypes pointer, a ctypes instance, or an integer memory address |
size | Number of bytes to copy. -1 (the default) means "stop at the first NUL byte" |
The return value is always bytes. The data is copied, so the result stays valid even after the original C memory is freed. The matching function for wide strings is ctypes.wstring_at(address, size=-1), which returns a str and counts characters (wchar_t units) instead of bytes.
The most common use is turning a char * returned by a C function into Python text. This example calls getenv from the C library on Linux or macOS.
Declaring the return type as c_void_p matters here. If you declared c_char_p, ctypes would already convert the result to bytes for you and you would not need string_at. Using c_void_p is the right choice when you need to free the pointer afterwards or when the buffer is not a plain NUL-terminated string.
When the data is binary, or when it may contain zero bytes, pass size explicitly. Without it, the read stops at the first 0x00.
This is the right form for image data, packets, structs and any buffer whose length is returned separately by the C API, for example (void *data, size_t length).
You can pass any ctypes object that has an address, or the integer address itself.
ctypes.addressof(obj) returns the integer address of a ctypes object, and ctypes.string_at accepts that integer directly. Note that the first form passes the array object and the second passes a plain integer, and both work.
| Need | Better choice |
|---|---|
| Copy bytes from an address | ctypes.string_at(address, size) |
| Read a wide string | ctypes.wstring_at(address, size) |
| Read from a ctypes array you own | array.raw or array.value |
| Zero-copy view of memory | memoryview(array) or ctypes.cast plus slicing |
| Fill an existing buffer | ctypes.memmove(dst, src, count) |
create_string_buffer objects expose .value (bytes up to the first NUL) and .raw (the whole buffer), so you rarely need string_at for memory that Python allocated. It earns its place when the memory belongs to C code and you only have an address.
string_at performs no validation. These mistakes are the usual causes of crashes or leaked data:
0 or already freed, which can raise a segmentation fault that kills the whole process.size on a buffer that is not NUL-terminated, so the read runs past the end of the buffer.Python raises an audit event named ctypes.string_at each time the function is called, so security tooling that uses sys.addaudithook can observe these reads. Treat ctypes as unsafe code, check for None or 0 before reading, and prefer a bounded read with an explicit size whenever the C API provides a length.
It returns bytes. Decode with .decode("utf-8") or the encoding the C library uses. For wide characters, use ctypes.wstring_at, which returns str.
Nothing protects you. An invalid address usually crashes the Python process with a segmentation fault or access violation, not a Python exception.
Yes. The bytes are copied into a new Python object, so changing or freeing the original C memory later does not affect your result.
c_char_p converts automatically, but you lose the original pointer, which you may need to free. Use c_void_p plus string_at when you must keep the address or the data is binary.
ctypes.string_at(address, size=-1) copies memory from an address into a bytes object, reading to the first NUL byte when size is omitted. Pass size for binary data, use wstring_at for wchar_t strings, and never call it with an address you have not validated. The function is a thin, unsafe bridge between C memory and Python, so keep the use narrow and copy data out as early as possible.