--- In [email protected], andrew clarke <[EMAIL PROTECTED]> wrote:
>
> On Tue 2008-02-12 16:24:20 UTC-0000, John Matthews ([EMAIL PROTECTED]) wrote:
>
> > Hi- in C, is it guaranteed that
> >
> > (unsigned)&ptr[1] - (unsigned)&ptr[0] == sizeof ptr[0]
>
> > Or do you have to allow for possible padding etc.?
>
> There is no padding. All blocks of memory allocated with malloc() are
> contiguous as far as the program is concerned.
>
> This may not actually reflect how those blocks of memory are stored on
> the hardware. What appears as contiguous memory in your C program may
> be fragmented (non contiguous) on the hardware itself, but the
> fragmentation is completely invisible to the program. malloc() does a
> lot of magical things behind the scenes to make this happen. :-)
Hi- thanks for your reply. Sorry, I don't think I made myself clear-
what I'm worried about is for example:
typedef struct
{
char c;
} Atype;
Say sizeof(Atype) == 1. Then:
Atype *ptr = malloc(10 * sizeof *ptr);
This allocates 10 bytes. But is it possible that the compiler pads
consecutive Atype structures such that:
(ptrdiff_t)&ptr[1] - (ptrdiff_t)&ptr[0] > 1 ?