[Lazarus] C Integer types

Hans-Peter Diettrich DrDiettrich1 at aol.com
Mon Jul 9 03:04:22 CEST 2012


Bernd K. schrieb:
> On 08.07.2012 17:18, Hans-Peter Diettrich wrote:
>> Bernd schrieb:
>>> 2012/7/7 Marco van de Voort <marcov at stack.nl>:
>> IMO all these members should be pointers, of size 64 bit according to 
>> the index increment (8 bytes?). Typecasts will be needed for 32 bit 
>> pointers, as demonstrated in above code.
> 
> Wait a monment, I have again looked at it. I think I was right and my
> other email admitting that I was wrong was wrong.
> 
> seqence_crc = *((int64_t*)data);
> seqence_key = *((int64_t*)&data[8]);
> 
> from the inside out:
> data is a pointer, say it points to a buffer

It's a variable, which can be an array.

> (int64_t*)data still points to that buffer
> *((int64_t*)data) are bytes 0..7 of that buffer as int64.

No, as "int64_t*", what's a "^int64" in Pascal.

> now the second line
> 
> seqence_key = *((int64_t*)&data[8]);
> 
> data[8] is the same as *(data+8)
> data[8] represents the 8th byte of that buffer (already dereferenced!)

It represents the 8th *element*, which here (most probably) is a byte. 
It could be something else, depending on the declaration of "data".

> &data[8] is the address of that byte

This address is untyped in C - that's the trick and purpose of the 
entire construct. In OPL you can have both typed or untyped @ operators, 
depending on compiler options, but here a typecast into the real data 
type is required in either case.

> &data[8] would be the same as (data+8)
> ((int64_t*)&data[8]) is still the same pointer

It's a pointer *starting* at that address.

> *((int64_t*)&data[8]) are bytes 8..17 of that buffer as int64

The size of the pointer depends on the target memory model (16/32/64 
bit). There is room for an 64 bit pointer in bytes 8..15, but how many 
bytes really are used depends on the sizeof(pointer).


> this should be equivalent:
> seqence_crc = *((int64_t*)&data[0]);
> seqence_key = *((int64_t*)&data[8]);
> compr_crc   = *((int64_t*)&data[16]);
> compr_len   = *((int32_t*)&data[24]);
> 
> and this also:
> seqence_crc = *((int64_t*)data);
> seqence_key = *((int64_t*)(data+8));
> compr_crc   = *((int64_t*)(data+16));
> compr_len   = *((int32_t*)(data+24));
> 
> Its really just a simple record with 4 numbers in it.

With 4 pointers in it, aligned to 8 byte boundaries.

DoDi





More information about the Lazarus mailing list