c++ - Dereferencing a 50% out of bound pointer (array of array) -
this new question in "i don't understand pointers in c , c++" collection.
if mix bits of 2 pointers equal values (pointing same memory address), happen have same bit representation, when 1 dereferenceable , 1 one past end, standard should happen?
#include <stdio.h> #include <string.h> #include <assert.h> // required: == b // returns copy of both , b dest // (half of bytes of either pointers) int *copy2to1 (int *a, int *b) { // check input: // not pointers must equal assert (a == b); // representation must match int *dest; size_t s = sizeof(dest); assert(memcmp(&a, &b, s) == 0); // copy , b dest: // on "exotic" architectures, size does't have dividable 2 size_t half = s/2; // = floor(s/2), char *pa = (char*)&a, *pb = (char*)&b, *pd = (char*)&dest; // copy half of dest: memcpy (pd, pa, half); // copy half of b dest: memcpy (pd+half, pb+half, s-half); // s-half = ceil(s/2) //printf ("a:%p b:%p dest:%p \n", a, b, dest); // check result assert(memcmp(&dest, &a, s) == 0); assert(memcmp(&dest, &b, s) == 0); return dest; } #define s 1 // size of inner array int main(void) { int a[2][s] = {{1},{2}}; int *past = a[0] + s, // 1 past end of inner array a[0] *val = &a[1][0], // valid dereferenceable pointer *mix = copy2to1 (past, val); #define print(x) printf ("%s=%p, *%s=%d\n",#x,x,#x,*x) print(past); print(mix); print(val); return 0; }
what want understand is: what "p points object x" mean?
see also
this question better version of previous questions array of arrays:
- is memcpy of pointer same assignment? variation on other question:
- dereferencing out of bound pointer contains address of object (array of array)
and other related questions pointer validity:
in [basic.compound]:
if object of type
t
located @ addressa
, pointer of type cvt*
value addressa
said point object, regardless of how value obtained.
past
, val
have same address, point same object. doesn't matter 1 "one past end" of first row , second first element of second row. there valid object @ address, here reasonable.
in c++17, of p0137, changes lot. now, [basic.compound] defines pointers as:
every value of pointer type 1 of following:
— pointer to object or function (the pointer said point object or function), or
— a pointer past end of object (5.7), or
— null pointer value (4.11) type, or
— invalid pointer value.
so now, past
value of 2nd type (a pointer past end of), val
value of 1st type (a pointer to). different categories of values , not comparable:
a value of pointer type pointer or past end of object represents address of first byte in memory (1.7) occupied object or first byte in memory after end of storage occupied object, respectively. [ note: pointer past end of object (5.7) not considered point unrelated object of object’s type might located @ address. pointer value becomes invalid when storage denotes reaches end of storage duration; see 3.7. —end note ]
past
doesn't point something, viewing contents if same val
no longer meaningful.
Comments
Post a Comment