Re: Book-keeping metadata for allocated memory



On 2006-03-15, David Resnick <lndresnick@xxxxxxxxx> wrote:

pinkfloydhomer@xxxxxxxxx wrote:
I was considering something along the lines of allocating the memory
for Container and thingy in one go, like:

Container* container = malloc(sizeof(Container) + size);

and then return something like

return (container + sizeof(Container));

But is that portable and well-defined? Or are there other solutions?

/David

A pointer returned by malloc is properly aligned for any use. But you
are returning an offset into the malloc'd block. I'm not sure how
you could guarantee that the offset is correctly aligned for your
needs, unless, as I read it, the things being allocated are always
structures. The standard says in section 6.2.5#25 "All pointers to
structure types shall have the same representation and alignment
requirements as each other.". I gather that means that offsetting
into a malloced block by the size of a structure will result in
pointer to memory correctly aligned for that structure, and hence for
other structures? If I'm wrong here, I'll no doubt be swiftly
corrected :)

It does sound a bit surprising. I tried this:

struct thing
{
char c;
};

int main(int argc, char **argv)
{
struct thing *t = malloc(100 * sizeof (struct thing));
printf("%p, %p, %d, %d\n", t, t+1, t+1 - t, sizeof (struct thing));
return 0;
}

and it printed out: 0x804a008, 0x804a009, 1, 1

So t+1 is not aligned on my system.

But there are various ways to guarantee the alignment for a particular
struct, as I suggested in an earlier post.

If the offset pointer needs to have the same alignment as a pointer
originally returned from malloc (i.e. maximum you ever might need), you
have to know what that is.

Practically speaking you probably just define a macro for it, but that's
not ideal, I wonder if there is a better way of determining your maximum
required alignment?
.



Relevant Pages

  • Re: Book-keeping metadata for allocated memory
    ... for Container and thingy in one go, ... Container* findContainer(void* thingy) ... A pointer returned by malloc is properly aligned for any use. ... But you are returning an offset into the malloc'd block. ...
    (comp.lang.c)
  • Re: Calculating storage alignment
    ... The allocation function returns a pointer to the object, which in reality will be a pointer to somewhere inside the chunk. ... I store an integer offset since the code uses a set of int flags to mark which objects within each chunk have already been allocated and this flag need to be cleared - this is why the number of objects per chunk is tied to the number of bits in an int. ... Just because the largest type is X chars, doesn't mean universal alignment is X chars. ...
    (comp.lang.c)
  • Re: Book-keeping metadata for allocated memory
    ... A pointer returned by malloc is properly aligned for any use. ... But you are returning an offset into the malloc'd block. ... Although all struct pointers have the same alignment ...
    (comp.lang.c)
  • Re: offsetof() macro
    ... > To my untrained eye that would basically result in a null pointer. ... The offsetofmacro is "special", as are many other things in the ... the structure definition, they know the offset of the first member, ... And the compiler ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... block size as a request for one byte, and let the alignment mechanisms raise it as they will. ... is therefore suitably aligned for any type (`struct s' in particular), the value can be assigned to any type of pointer, and can then be used to access an object of that type. ... of the Standard can distort its meaning. ...
    (comp.lang.c)