Topic: Memory Allocation (was auto_ptr efficiency)


Author: paul.black@vf.vodafone.co.uk
Date: 1996/11/22
Raw View
fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson) wrote:
[I wrote:]
> >>>Also, if the class has very simple attributes (e.g. all chars) then
> >>>class new & delete operators might not (and do not need to) provide a
> >>>block of memory with the same alignment properties as the global
> >>>operators.
>
> That's not correct, because all allocation functions must satisfy
> the requirements in 3.7.3:
>
> |   3.7.3  Dynamic storage duration                    [basic.stc.dynamic]
> |
> | 3 Any allocation and/or deallocation functions defined in a C++  program
> |   shall conform to the semantics specified in this subclause.
> |
> |   3.7.3.1  Allocation functions           [basic.stc.dynamic.allocation]
> [...]
> | 2 The function shall return the address of the start of a block of stor-
> |   age whose length in bytes shall be at least as large as the  requested
> |   size.  [...]  The pointer returned shall  be  suit-
> |   ably  aligned  so that it can be assigned to a pointer of any type and
> |   then used to access the object  or  array  in  the  storage  allocated

Yikes! Does this mean when I write code, as well as being portable,
maintainable, blah blah blah, it must now comply with the ANSI C++ standard?

Consider:

class A
{
    .
    .
    char a1;
    char a2;
    char a3;
    .
    .

    void *operator new(size_t);
    void operator delete(void *);
};

static char array[n * sizeof(A)];

void *A::operator new(size_t s)
{
    // Get an x

    return (array + x * sizeof(A));
}

If x is 1 , the A::operator new will return 'array + 3'. However, this
would not satisfy the alignment requirements of most processors used
today.

I must therefore assume that the part of the standard quoted above
refers to any new & delete supplied with the compiler rather than those
written by programmers - the ANSI compliance of a compiler being
dependent on the source code it was compiling seems harsh to the
compiler vendors!

Paul
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/23
Raw View
paul.black@vf.vodafone.co.uk writes:

>Yikes! Does this mean when I write code, as well as being portable,
>maintainable, blah blah blah, it must now comply with the ANSI C++ standard?

Well, strictly speaking, you don't _have_ to write code that complies
with the C++ standard, just as you don't _have_ to write code that is
portable or maintainable.  However, making your code conform to the
relevant standard is usually the only way you can be sure that it will
be portable.  But that applies to any language, not just C++, and it's
not something new.

>Consider:
>
>class A
>{
>    .
>    .
>    char a1;
>    char a2;
>    char a3;
>    .
>    .
>
>    void *operator new(size_t);
>    void operator delete(void *);
>};
>
>static char array[n * sizeof(A)];
>
>void *A::operator new(size_t s)
>{
>    // Get an x
>
>    return (array + x * sizeof(A));
>}
>
>If x is 1 , the A::operator new will return 'array + 3'. However, this
>would not satisfy the alignment requirements of most processors used
>today.

Yes, and hence that code does not conform to the (draft) standard.

Incidentally, I think some compilers always align structs on word
boundaries, even if the struct only has `char' members, and certainly
compilers are permitted to do this, so the above code would not be
conforming anyway, even if it weren't for the requirement that
operator new return memory appropriately aligned for any type
of the speciifed size, rather than just for the specified type.

>I must therefore assume that the part of the standard quoted above
>refers to any new & delete supplied with the compiler rather than those
>written by programmers -

No, it refers to both.

>the ANSI compliance of a compiler being
>dependent on the source code it was compiling seems harsh to the
>compiler vendors!

For a user-defined opertator `new', the quoted part of the standard
determines the compliance of the user's code, not that of the
compiler.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]