解引用50%的外边界指针(数组的数组) [英] Dereferencing a 50% out of bound pointer (array of array)
问题描述
这是在我的收藏中的C和C ++我不明白指针的新问题。
This is a new question in my "I don't understand pointers in C and C++" collection.
如果我混用两个指针相等的值(指向相同的内存地址)的位,碰巧有完全重新presentation相同的位,当一个人提领,一个一个过去到底有什么没有标准的发言权应该发生?
If I mix the bits of two pointers with equal values (pointing to the same memory address), that happen to have exactly the same bit representation, when one is dereferenceable and one is one past the end, what does the standard say should happen?
#include <stdio.h>
#include <string.h>
#include <assert.h>
// required: a == b
// returns a copy of both a and b into dest
// (half of the bytes of either pointers)
int *copy2to1 (int *a, int *b) {
// check input:
// not only the pointers must be equal
assert (a == b);
// also the representation must match exactly
int *dest;
size_t s = sizeof(dest);
assert(memcmp(&a, &b, s) == 0);
// copy a and b into dest:
// on "exotic" architectures, size does't have to be dividable by 2
size_t half = s/2; // = floor(s/2),
char *pa = (char*)&a, *pb = (char*)&b, *pd = (char*)&dest;
// copy half of a into dest:
memcpy (pd, pa, half);
// copy half of b into 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, // one past the 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;
}
我真正想了解的是:什么是p指向对象X是什么意思
请参见
这个问题是一个更好的版本约数组的数组我的previous问题:
This question is a better version of my previous questions about array of arrays:
- 是一个指针的memcpy一样分配这是对我的其他问题的变化:
- 访问一个出界的指针包含一个对象的地址(数组的数组)
- Is memcpy of a pointer the same as assignment? which is a variation on my other question:
- Dereferencing an out of bound pointer that contains the address of an object (array of array)
和有关指针有效性等相关问题:
and other related questions about pointer validity:
- 是指针变量只是一些运营商的整数或者是他们&QUOT;神秘&QUOT;?
- 和C ++的问题:覆盖对象与同类型 的对象
- Are pointer variables just integers with some operators or are they "mystical"?
- and the C++ question: Overwriting an object with an object of same type
推荐答案
在[basic.compound]:
In [basic.compound]:
如果类型的对象 T
位于地址 A
,类型的指针的 CV 的 T *
,其值为
地址 A
据说指向对象的不管是如何得到的值
If an object of type
T
is located at an addressA
, a pointer of type cvT*
whose value is the addressA
is said to point to that object, regardless of how the value was obtained.
过去
和 VAL
有相同的地址,所以它们指向同一个对象。没关系,一个是一过去的结束的第一行,第二个是第二行的第一个元素。有在该地址有效的对象,因此,这里的一切是完全合理的。
past
and val
have the same address, so they point to the same object. It doesn't matter that one is "one past the end" of the first row and the second is the first element of the second row. There is a valid object at that address, so everything here is perfectly reasonable.
这篇关于解引用50%的外边界指针(数组的数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!