采取未初始化指针的地址是否是未定义的行为? [英] Is it undefined behavior to take the address of an uninitialized pointer?

查看:175
本文介绍了采取未初始化指针的地址是否是未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N1570指出这是未定义的行为:

N1570 states that this is undefined behavior:

§J.2/1使用具有自动存储持续时间的对象的值 而不确定(6.2.4、6.7.9、6.8).

§J.2/1 The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8).

在这种情况下,我们的指针具有不确定的值:

And in this case, our pointer has an indeterminate value:

§6.7.9/10如果具有自动存储持续时间的对象不是 显式初始化,其值不确定.如果有物体 静态或线程存储持续时间未明确初始化, 然后:

§6.7.9/10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

-如果具有指针类型,则将其初始化为空指针;

— if it has pointer type, it is initialized to a null pointer;

然后我假设以下测试程序表现出未定义的行为:

I then presume the following test program exhibits undefined behavior:

#include <stdio.h>

int main(void) {
    char * ptr;
    printf("%p", (void*)&ptr);
}

我的动力是strtol功能.首先,让我引用N1570与endptr参数有关的部分:

My motivating concern is the strtol function. First, let me quote the parts of N1570 related to the endptr parameter:

§7.22.1.4/5如果主题序列具有预期的形式,并且 base的值是零,以 根据 6.4.4.1的规则[...]指向最终字符串的指针存储在 endptr指向的对象,前提是endptr不为null 指针.

§7.22.1.4/5 If the subject sequence has the expected form and the value of base is zero, the sequence of characters starting with the first digit is interpreted as an integer constant according to the rules of 6.4.4.1. [...] A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

§7.22.1.4/7如果主题序列为空或没有 预期形式,不执行任何转换; nptr的值为 存储在endptr指向的对象中,前提是endptr为 不是空指针.

§7.22.1.4/7 If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

这意味着endptr需要指向一个对象,并且endptr在某些时候已被取消引用.例如,此实现做到了:

This implies that endptr needs to be pointing to an object, and also that endptr is at some point dereferenced. For example, this implementation does so:

if (endptr != 0)
    *endptr = (char *)(any ? s - 1 : nptr);

但是,此获得高度评价的答案以及

Yet, this highly upvoted answer as well as this man page both show endptr being passed to strtol uninitialized. Is there an exception which makes this not undefined behavior?

推荐答案

在此表达式中:

&ptr

&操作数的地址,即i.例如,产生了ptr对象的地址,但从未评估ptr对象.

the address of the & operand, i. e., the address of the ptr object is yielded but the ptr object is never evaluated.

(C11,6.3.2.1p2)除非它是sizeof运算符的操作数,否则一元&运算符,++运算符,-运算符或.运算符或赋值的左操作数运算符,将不具有数组类型的左值转换为存储在指定对象中的值(不再是左值);这称为左值转换."

(C11, 6.3.2.1p2) "Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue); this is called lvalue conversion."

这篇关于采取未初始化指针的地址是否是未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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