Skip to content

[basic.compound] p5 A deallocation cannot happen before itself #897

@xmh0511

Description

@xmh0511

Full name of submitter (unless configured in github; will be published with the issue): Jim X

[basic.compound] p5 says:

A pointer value P is valid in the context of an evaluation E if P is a pointer to function or a null pointer value, or if it is a pointer to or past the end of an object O and E happens before the end of the duration of the region of storage for O. If a pointer value P is used in an evaluation E and P is not valid in the context of E, then the behavior is undefined if E is an indirection ([expr.unary.op]) or an invocation of a deallocation function ([basic.stc.dynamic.deallocation])

[basic.stc.dynamic.deallocation] p5 says:

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value ([basic.compound]), the deallocation function shall deallocate the storage referenced by the pointer, ending the duration of the region of storage.

Consider this example:

int main(){
  auto p = operator new(sizeof(char)); // #0
  operator delete(p); // #1
}

#1 is an invocation of a deallocation function, and it ends the duration of the region of storage referenced by p. However, according to [intro.races] note 7

An evaluation does not happen before itself.

So, p is not valid in #1, therefore, the program has UB?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions