gpt4 book ai didi

c++ - 在 C/C++ 中取消初始化变量

转载 作者:太空狗 更新时间:2023-10-29 19:43:32 25 4
gpt4 key购买 nike

这与其说是一个实际问题,不如说是一个理论问题,但我想知道是否可以在 C(或 C++)中取消初始化一个变量。假设我们有以下代码:

void some_fun()
{
int a; // <-- Here a is an un-initialized variable, it's value is the value
// found in the memory at the given location.
a = 7; // Now I have initialized it

// do something with a ...

// and here do something again with a that it
// will have the same state (ie: indeterministic) as before initialization
}

(不,我不想在 a 中放入一个随机值,因为那也是一个初始化,也不是 0,因为这是一个非常好的值,...我只是想要它再次处于初始化之前的“我对此一无所知”阶段。

(是的,我知道:What happens to a declared, uninitialized variable in C? Does it have a value?)

最佳答案

您可以使用 setjmp()longjmp()通过对代码进行一些重新排列来获得您想要的行为。下面的代码初始化 a为 1,以便打印语句不会调用未定义的行为。

jmp_buf jb;

void some_func (void)
{
int a = 1;

if (setjmp(jb) == 0) {
a = 7;

// do something
printf("a = %d (initialized)\n", a);

// use longjmp to make `a` not initialized
longjmp(jb, 1);
// NOTREACHED
} else {
printf("a = %d (not initialized)\n", a);
}
}

longjmp()调用返回到 setjmp() 的已保存上下文, 并移动到 else案例意味着a尚未初始化。

当使用 GCC 优化编译时,上述函数输出:

a = 7 (initialized)
a = 1 (not initialized)

如果您希望在不启用优化的情况下执行此操作,请尝试添加 register存储类为 a的声明。

A demo.

更详细的解释

那么,为什么我会想到 setjmp()longjmp()会工作?这是 C.11 §7.13 ¶1-2 必须说的:

The header <setjmp.h> defines the macro setjmp, and declares one function and one type, for bypassing the normal function call and return discipline.

The type declared is

jmp_buf

which is an array type suitable for holding the information needed to restore a calling environment. The environment of a call to the setjmp macro consists of information sufficient for a call to the longjmp function to return execution to the correct block and invocation of that block, were it called recursively. It does not include the state of the floating-point status flags, of open files, or of any other component of the abstract machine.

这说明应该发生的是 longjmp回到保存在 jmp_buf 中的上下文调用 setjmp就像运行到 longjmp 之前的代码一样调用是一个递归函数调用,longjmp就像从递归回调中返回 setjmp .对我来说,这意味着自动变量将“未初始化”。

    int a;

// the following expression will be false if returning from `longjmp`
if (setjmp(jb) == 0) {
// this section of code can be treated like the `setjmp` call induced
// a recursive call on this function that led to the execution of the
// code in this body
a = 7;
//...
// assuming not other code modified `jb`, the following call will
// jump back to the `if` check above, but will behave like a recursive
// function call had returned
longjmp(jb, 1);
} else {
// `a` expected to be uninitialized here
}

但是,似乎有一个陷阱。来自 C.11 §7.13.2 ¶3:

All accessible objects have values, and all other components of the abstract machine have state, as of the time the longjmp function was called, except that the values of objects of automatic storage duration that are local to the function containing the invocation of the corresponding setjmp macro that do not have volatile-qualified type and have been changed between the setjmp invocation and longjmp call are indeterminate.

a是本地的,不是 volatile 限定的,并且已在 setjmp 之间更改和 longjmp调用,它的值是不确定的,即使它在调用 setjmp 之前被正确初始化也是如此!

因此,使用 longjmp回到本地 setjmp修改自动非 volatile 变量后,返回到 setjmp 点后,总是会导致这些修改后的变量“未初始化” .

关于c++ - 在 C/C++ 中取消初始化变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25502731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com