C type buffer copy

Neil Hodgson nyamatongwe+gravity at gmail.com
Tue Mar 22 17:27:34 EST 2005


Joe:

> testCode(unsigned char buf, unsigned long len)
> {
>     unsigned long data=0x0;
>     while (len--)
>     {
>         *buf++ = (unsigned char)data++

   This C code will crash since buf is declared as an unsigned char, not 
an unsigned char*. Stop thinking in terms of translating low level C 
because Python is a higher level language and does not have machine 
address pointers. You should be implementing the intent of your design 
which we can't determine from tiny incorrect snippets. For example, we 
can't determine if the intent is to create a new string, translation: 
''.join([chr(x) for x in range(len)]), or if you are modifying an 
existing sequence or inserting into a sequence. It is also impossible to 
tell if you should be using a list, array or string for this job.

   Neil



More information about the Python-list mailing list