Search code examples
pythoncpointersmemset

Memset() in Python, clearing buffer


I trying convert some C code to Python and have problem with clearing buffer (data which will be send by UDP). Function to convert looks like:

void func(Struct *data_1, Struct *data_2)
{
    data_p1 = data_1;
    data_p2 = data_2;
    memset(datap1, 0, sizeof(*data_p1));
    memset(datap2, 0, sizeof(*data_p2));
}

And I have to problms,I don't know how to replace c pointers and is that crucial to replace them. Second problem is that memset, I know that is ctype but I don't know how to properly use it. Can anyone help?


Solution

  • When converting code between different languages, never try to convert at the expression level, but wonder what the code is actually doing. Here the C code sets the content of a struct to NULL bytes. There is no direct equivalent in Python, full stop. The closer equivalent way would be to set all object attributes of a Python object to None, set integers to 0 and floats to 0.0.