#Copy pointer to struct at address
215 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
you likely shouldn't copy a FooStruct* to a BarStruct*
im keeping different datatypes in the same allocation
src - 4 is a pointer to a FooStruct**, but src is a BarStruct*
in a char* you mean ?
yeah
are you sure the -4 here is correct ?
yes
do you mean 4 char before, or 4*sizeof(FooStruct*) char before?
4 chars before
i can show you what im referencing
i need to copy the PyDictValues** to a seperate allocation
whats wrong with this?
*((PyDictValues***) dest) = ((PyDictValues**) src) - 4;
how do you know src -4 is a FooStruct**
.
this is your goal?
yes
this isn't allocating any new memory
gotcha
i just need to put the pointer in it
what type of pointer is dest
PyObject*
(src is also a PyObject*)
have u tried using the Python C API
this is the python c api
Then why are you directly manipulating pointers src -4 the way u are
what's your end goal
im trying to artifically copy a PyObject* to an allocation
which in order to do the PyObject* must have attribute values at loc - 4
then ur just copying the reference you need a seperate instance
read this
im trying to copy the reference
have you tried the Py_INCREF function
u need to make a deep copy
copy does not let me copy an object into an allocation
is it just plain not possible to set the value from src - 4?
u want to duplicate the object's content into somewhere else right
somewhere already allocated
u can do *dest = copiedObject
if dest is a PyObject**
but then dest wont include that important PyDictValues**
typedef struct {
PyObject* obj;
PyDictValues **dictValues;
} thing;
Py_INCREF(src);
thing->obj = src;
thing->dictValues = /* Somehow obtain PyDictValues** for src, if src is a dict */;
@stuck nimbus im not even sure what u are trying to do atp
but this is my guess
thing would not be a valid python object then
im trying to take an object, and copy all of its contents into a memory allocation, so that allocation can then be used as a valid python object
ok
that makes more sense
i was a lil confused
u would need to implement a custom constructor
which would duplicate the contents of ur object
but im not sure how
u might want to ask someone more into python c api
i already implemented copying the PyObject* part of it, but python complains about it having no attributes since the PyDictValues** wasnt copied with it
dest + 4 is the python object part, and dest should be the PyDictValues**
no where can i find that PyDictValues is anywhere in the python c api
its in dictobject.h iirc
have you tried using PyObject_SetAttrString or PyDict_Copy
neither of those would help me here
all i need to do is just copy that pointer to my allocation
src - 4 to dest
PyObject* src_dict = PyObject_GetAttrString(src, "__dict__");
if (src_dict && PyDict_Check(src_dict)) {
PyObject* copied_dict = PyDict_Copy(src_dict);
if (copied_dict) {
PyObject* dest_obj = *((PyObject**)(dest + 4));
PyObject_SetAttrString(dest_obj, "__dict__", copied_dict);
Py_DECREF(copied_dict);
}
Py_DECREF(src_dict);
}
i found this online asw
an example implementation of copying (in your case)
i dont want to copy the dict
i want both objects to point to the same dict
which is weird but im more concerned with whether i could than whether i should
cant u just explicitly set the__dict__attribute of the second object to be the same as the first?
or am i missing something
__dict__ is stored under PyObject.ob_type->tp_dict
which holds the types dict
but the instance attributes are stored at obj - 4
no, the type dict is copied already
just the pydictvalues?
after thinking about it for a wihle i might be overthinking it
have you tried using memcpy
memcpy(*dest, &sourceValues, sizeof(PyDictValues*));
void *objectDest = (char*)*dest + sizeof(PyDictValues*);
memcpy(objectDest, &sourceObject, sizeof(PyObject*));
``` did you try something like this?
where sourcevalues is ur src
oh
hm
char *b = (char*) dest;
memcpy(b, your_dict_values, sizeof(PyDictValues*));
memcpy(b + sizeof(PyDictValues*), &your_py_obj, sizeof(PyObject*));
try that
should your_dict_values be the PyDictValues**?
ye
wouldnt that make it a triple pointer?
oh its a double
im assuming that your_py_obj shouldnt have the addressof either?
your_py_object in my case is a PyObject*
remove the address operator
on your_dict_values
got it
no it should
ok
did it work?
hmm, src - 4 and dest are different pointers
src - 4: 0x78027ecdaf90, dest: 0x59862056c250
didn't you say they were the same
oh u want them to be the same
my b
idk if u could achieve that
i dont think u can
why not?
at least not the way i set it up
do they both point to the same PyDictValues*?
your_dict_values is copied at the start of dest, so whatever PyDictValues* it was pointing to, now a copy of that pointer is stored at dest.
your_py_obj's address is stored right after that, so you now have a pointer to your_py_obj stored next to it
they dont both point to a pydictvalues *
one is a pydictvalue * one is a pyobject*
thats why im not sure u can do what u want to do
why cant the pointer from src - 4 just get copied to dest?
i think youre overthinking a little
you mean what its pointing too?
because they aren't the same pointer type
one struct could be bigger so -4 isn't guranteed
dest should be a PyDictValues**, but dest + 4 is a PyObject*
so ur saying u have an array of pydictvalues pointers and the 4th one is a pyobject *
no
you mean *(dest + 4) is a pyobject*
ah
yes
and dest + 4 is a PyObject*
the entire dest allocation itself contains multiple types
PyDictValues **dest = array of PyDictValues pointers
so (PyObject*) (dest + 4) should be a PyObject *
dest is just a void*, but the value at dest should be a PyDictValues**
yes
yea i mis understood u from the start then
lol
let me re read what u asked
so you want to copy a pyobject * allocation to dest + 4
correct?
yes
and you are sure dest + 4 holds a pyobject *
if you are, then you can just do
PyObject * newObj; // whatever u want to copy over
(PyObject *) *(dest + 4) = newObj;
and ive already implemented that
sorry you need to dereference it because dest + 4 is still a double pointer
it’s getting that PyDictValues** at dest that i can’t do
*(dest + 4) is a PyObj *
this code only copies the PyObject*
but i need to copy the PyDictValues** from src - 4
yea because *(dest + 4) holds a PyObject*
yup
so how do i do this then
does it matter where in dest you put it?
if dest is type void** you can just either allocate more space at the end and typecast it to PyDictValues** and directly copy the data there
i dont know where u want to put the data specifically in dest from src-4
it needs to be at the start
yes
okay
that can be achieved
void **dest; (ur block of memory);
size_t newSize = sizeof(dest**) + sizeof(PyDictValues**);
PyDictValues **comesFirst; // your memory u want to come first
void** newMemory = malloc(newSize);
memcpy(newMemory, &comesFirst, sizeof(comesFirst));
memcpy((char*)newMemory + sizeof(comesFirst), dest, sizeof(dest));
dest = newMemory; // dest is still ur pointer
@stuck nimbus try that
this is what that is doing
src - 4 and dest are still different pointers
src is your PyDict** right
why do you need it to be in this specific order anyways?
you want src-4 (your pydict) to be the first block of memory right? in dest
@stuck nimbus
because thats how python reads it
yes
are you doing
PyDictValues** stuff = src - 4;
and then copying stuff over to dest
if not try that ^
I already tried that yeah
are you 100% src-4 is a PyDictValue** and src is a PyObj*
yes
sorry for late reply
how do you define your src var
then src -4 and dest both point to the same thing
they should be the same
ill try and replicate the code give me a sec
any luck?
for some reason it's not the same
the addresses are very close but not the same
im not sure if it's because of the type casting or what