Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-09

memory layout of arrays

6 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marc Santhoff

Hi,

when using arrays in C the layout of arrays is sort of flat, since pointer arithmetics for accessing values works (e.g. "pointer += 4" for 32 bit values).

Is this the same for pascal/fpc? Do values of for example an integer array sit in memory at 4 Byte boundaries?

I need to hand over arrays to a C library and have no influence on how access is done internally (still und investigation). Currently the C lib reads garbage from my array ...

TIA,
Marc

Reply by email

Jonas Maebe
to Marc Santhoff
Marc Santhoff wrote
I need to hand over arrays to a C library and have no influence on how access is done internally (still und investigation). Currently the C lib reads garbage from my array ...

That means that the type you use in Pascal is probably different from the type used in C.

Jonas

Reply by email

Michael Van Canneyt
to Marc Santhoff: when using arrays in C the layout of arrays is sort of flat, since pointer arithmetics…
Marc Santhoff wrote 8 lines
Hi, when using arrays in C the layout of arrays is sort of flat, since pointer arithmetics for accessing values works (e.g. "pointer += 4" for 32 bit values). Is this the same for pascal/fpc? Do values of for example an integer array sit in memory at 4 Byte boundaries?

For integers: normally, yes.

For elements that are smaller than integers, this may depend on the size and the value of the {$Align} directive. But some of the compiler specialists should confirm this.

(and then I'll add a note on this in the programmer's manual)

Michael.

Reply by email

Jerry
to Marc Santhoff: when using arrays in C the layout of arrays is sort of flat, since pointer arithmetics…
Marc Santhoff wrote 14 lines
Hi, when using arrays in C the layout of arrays is sort of flat, since pointer arithmetics for accessing values works (e.g. "pointer += 4" for 32 bit values). Is this the same for pascal/fpc? Do values of for example an integer array sit in memory at 4 Byte boundaries? I need to hand over arrays to a C library and have no influence on how access is done internally (still und investigation). Currently the C lib reads garbage from my array ...

FWIW, GPC arrays seem to be laid out exactly like those for C. I have passed arrays to and from Ada by specifying "C conventions," so at least with GPC things went well. I haven't tried this with FPC, however. Beware of 2D arrays in C because there seems to be no requirement in C that all of the data be allocated in contiguous memory; the parts of the 2D array (most likely, rows) can in effect be fragmented. I am told that some or most C compilers do allocate the memory as one big chunk, however.

TIA, Marc

Reply by email

Vinzent Hoefler
to Jerry
Jerry wrote
however. Beware of 2D arrays in C because there seems to be no requirement in C that all of the data be allocated in contiguous memory;

Most probably yes. This applies to all mutidimensional arrays in C. The reason is simple, arrays and pointers are handled quite the same way, so there's an automatic dereferencing for each array subscript...

Described in Pascalish terms it would be a N-dimensional array of pointers to an (N-1)-dimensional array of pointers to an (N-2)-dimensional array ...

Vinzent.

Reply by email

Marc Santhoff
to Jonas Maebe: That means that the type you use in Pascal is probably different from the type used in C.…
Jonas Maebe wrote 9 lines
On 14 Sep 2006, at 11:00, Marc Santhoff wrote: > I need to hand over arrays to a C library and have no influence on how > access is done internally (still und investigation). Currently the > C lib > reads garbage from my array ... That means that the type you use in Pascal is probably different from the type used in C.

You got me, I've looked 20 times and did not notice using integer for my array, now it is longint and anything works fine!

Btw., is there any function for asking for the internal type of a variable? Something like "typeof(anyvar)" spitting out a type code or string saying what memory type this is?

Okay, for integers sizeof() would at least tell the number of bytes ... but nothing about alignment and no idea about float values.

Thank you all,
Marc

Reply by email