Search code examples
clinuxcpu-wordlong-integerptrace

Does the size of a long equal to the size of a word


Hello I'm programming under Linux ( In C ).

When i use ptrace() to read data, it returns a word. In all the examples I see people using a long to read the input. Does a Long always have the same size of a word? I know that a word is the natural size with which a processor is handling data (the register size). But does that also apply to long's on different architectures etc?

 OValue_t outputValue;
 //.su_word is a long
 outputValue.su_word = ptrace(PTRACE_PEEKDATA,Process.ProcId,address,0); 
 printf("word  : %ld\n", outputValue.su_word);
 printf("int8 : %i\n", outputValue.su_int8);

EDIT: Thanks to Krzysztof Kosiński/unwind and the answer by Jonathan Leffler here I understand that ptrace returns a long and a long is big enough for a word.

http://docs.oracle.com/cd/E19620-01/805-3024/lp64-1/index.html


Solution

  • The Linux API defines ptrace to always return long.

    long ptrace(enum __ptrace_request request, pid_t pid,
                void *addr, void *data);
    

    On Linux, the size of long is equal to the machine word size (32-bit on 32-bit machines, 64-bit on 64-bit machines, and so on). As far as I know, this is true on all major architectures which have Linux ports.

    Note that this is not true on Windows, where long is 32-bit even on x64 - but since ptrace is a Linux-specific call, you don't have to worry about it.