Jona
Jona

Reputation: 1825

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

Upvotes: 2

Views: 1224

Answers (3)

Krzysztof Kosiński
Krzysztof Kosiński

Reputation: 4335

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.

Upvotes: 3

Phil Perry
Phil Perry

Reputation: 2130

The only rule is that a long must be at least the size of a word (int). Beyond that it's up to the machine architecture and the compiler writer. You could legally have char = short = int = long = long long.

Upvotes: -2

unwind
unwind

Reputation: 400019

Examples use long since that's how the function is documented to work, the prototype is:

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

There's even a note in the manual page saying:

The size of a "word" is determined by the operating-system variant (e.g., for 32-bit Linux it is 32 bits).

I think it just as well have been declared to return int, since int is supposed to be a platform's "natural" integer size, which I think is the "word size" for that platform in typical cases.

The function does not assume that the long has more precision than "a word", as far as I could tell from the manual page. It uses a return value of -1 to signal errors, but since that can of course be a valid value as well, requires you to also check errno.

Upvotes: 4

Related Questions