Discussion:
Array-to-pointer conversion for arrays outside their lifetime
(too old to reply)
Language Lawyer
2018-08-29 13:06:50 UTC
Permalink
Is it allowed (well-defined) to apply array-to-pointer conversion to a glvalue denoting an array object outside its lifetime? (For example, inactive union member).
Is pointer arithmetic defined for the result of the conversion?


Related question is: is pointer arithmetic well-defined if array is within its lifetime, but some elements (most importantly — the first one) are not within their lifetimes? I think the intent is that it well-defined, because it is desirable for using arrays to provide storage. Example from the standard:

template<typename ...T>
struct AlignedUnion {
alignas(T...) unsigned char data[max(sizeof(T)...)];
};
int f() {
AlignedUnion<int, char> au;
int *p = new (au.data) int; // OK, au.data provides storage
char *c = new (au.data) char(); // OK, ends lifetime of *p
char *d = new (au.data + 1) char();
return *c + *d; // OK
}

Creation of the int object ends the lifetime of the first sizeof(int) array members. But the pointer `au.data`, pointing to the object outside its lifetime, is used for pointer arithmetic: `au.data + 1`.

From [basic.life]/6: "… Otherwise, such a pointer refers to allocated storage ([basic.stc.dynamic.allocation]), and using the pointer as if the pointer were of type void*, is well-defined…"
`au.data + 1` is definitely not a usage of a pointer as if it were of type void*.
[basic.life]/6 has a list of cases of pointer usage for which the behavior is undefined. Is the list closed?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Language Lawyer
2018-09-13 17:03:22 UTC
Permalink
Post by Language Lawyer
Is it allowed (well-defined) to apply array-to-pointer conversion to a glvalue denoting an array object outside its lifetime? (For example, inactive union member).
https://groups.google.com/a/isocpp.org/d/msg/std-discussion/JbWQT-AOxzc/ChrgDVjbAwAJ R. Smith told that it is not defined.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Continue reading on narkive:
Loading...