Skip to content

Add lower_bounds in CPtrToPointer #1822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2023
Merged

Conversation

czgdp1807
Copy link
Collaborator

Closes #1798

@@ -523,6 +528,8 @@ def __setattr__(self, name: str, value):
def c_p_pointer(cptr, targettype):
targettype_ptr = convert_type_to_ctype(targettype)
if isinstance(targettype, Array):
if py_is_dataclass(targettype._type):
return ctypes.cast(cptr.value, ctypes.py_object).value
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cptr.value is nothing but the address of foos: Foo[1] = empty(1, dtype=Foo). And ctypes.cast(cptr.value, ctypes.py_object).value just gives the original numpy array back, so effectively it becomes a pointer and you can easily make changes to it in other functions.

That's the only way forward if we want to keep using numpy arrays for arrays of dataclasses. If we can avoid using numpy arrays for dataclasses then we can define our own empty in lpython.py or in our own version of numpy.py. In our empty we can just create a ctypes.Structure array and return it via empty. For all the types like i32, etc we can keep calling numpy.empty. But I would not go for it unless extremely necessary.

@certik certik merged commit 1aad65e into lcompilers:main May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

casting between struct array and CPtr causes lpython to segfault despite generating valid C code
2 participants