C标准是否允许将任意值分配给指针并递增? [英] Does the C standard permit assigning an arbitrary value to a pointer and incrementing it?

查看:73
本文介绍了C标准是否允许将任意值分配给指针并递增?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码的行为定义是否正确?

Is the behaviour of this code well defined?

#include <stdio.h>
#include <stdint.h>

int main(void)
{
    void *ptr = (char *)0x01;
    size_t val;

    ptr = (char *)ptr + 1;
    val = (size_t)(uintptr_t)ptr;

    printf("%zu\n", val);
    return 0;
}

我的意思是,即使指针指向某个随机地址,我们也可以为其分配一些固定的数字并使其递增吗?(我知道您不能取消引用它)

I mean, can we assign some fixed number to a pointer and increment it even if it is pointing to some random address? (I know that you can not dereference it)

推荐答案

分配:

void *ptr = (char *)0x01;

实现定义的行为,因为它会将整数转换为指针. C标准关于指针:

Is implementation defined behavior because it is converting an integer to a pointer. This is detailed in section 6.3.2.3 of the C standard regarding Pointers:

5 整数可以转换为任何指针类型.除非先前指定,否则结果是实现定义的,可能未正确对齐,可能未指向实体类型,可能是陷阱的表示形式.

5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

至于后续的指针算法:

ptr = (char *)ptr + 1;

这取决于几件事.

首先,根据上述6.3.2.3, ptr 的当前值可能是陷阱表示.如果是,则行为为未定义.

First, the current value of ptr may be a trap representation as per 6.3.2.3 above. If it is, the behavior is undefined.

接下来是 0x1 是否指向有效对象的问题.仅当指针操作数和结果都指向数组对象的元素(单个对象算作大小为1的数组)或数组对象后面的一个元素时,才添加指针和整数才有效.这将在6.5.6节中详细介绍:

Next is the question of whether 0x1 points to a valid object. Adding a pointer and an integer is only valid if both the pointer operand and the result point to elements of an array object (a single object counts as an array of size 1) or one element past the array object. This is detailed in section 6.5.6:

7 就这些运算符而言,指向不是数组元素的对象的指针的行为与指向类型为long的数组的第一个元素的指针对象的元素类型

7 For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type

8 将具有整数类型的表达式添加到指针或从指针中减去时,结果将具有指针的类型操作数.如果指针操作数指向数组的元素对象,并且数组足够大,结果指向一个元素与原始元素的偏移量,使得结果数组元素和原始数组元素的下标等于整数表达式.换句话说,如果表达式 P 指向数组对象的第i个元素,表达式(P)+ N (等效为 N +(P))和(P)-N (其中 N 的值为n)指向,数组对象的第i + n个元素和第i-n个元素(如果存在).此外,如果表达式P指向数组对象,表达式(P)+1 指向对象的最后一个元素数组对象,如果表达式 Q 指向数组对象的最后一个元素,表达式(Q)-1 指向数组对象的最后一个元素.如果两个指针都操作数和结果指向同一数组的元素对象,或者在数组对象的最后一个元素之后评估不应产生溢出;否则,行为是未定义.如果结果指向最后一个元素的最后一个数组对象,不得将其用作一元操作数*被评估的运算符.

8 When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P) ) and (P)-N (where N has the value n ) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

在托管实现中,值 0x1 几乎可以肯定地 not 指向有效对象,在这种情况下,附加值是未定义.但是,嵌入式实现可能支持将指针设置为特定值,如果是这样,实际上 0x1 确实指向有效对象的情况可能会发生.如果是这样,则该行为定义明确,否则为未定义.

On a hosted implementation the value 0x1 almost certainly does not point to a valid object, in which case the addition is undefined. An embedded implementation could however support setting pointers to specific values, and if so it could be the case that 0x1 does in fact point to a valid object. If so, the behavior is well defined, otherwise it is undefined.

这篇关于C标准是否允许将任意值分配给指针并递增?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆