未初始化的本地字符的行为? [英] Behavior of uninitialized local char?

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

问题描述

如果你可以说一个未初始化的本地 int ,那么它得到一个未定义的值,但是如果你有一个本地的 char 变量应该没有未定义的值?当然0可能是那个未定义的值,但我想知道char是否有任何不同,因为我发现的所有相关信息都是关于 int 的,下面的程序只是输出0 char变量被转换为int。我使用GCC 4.7没有标志。

If you have lets say a local int that is uninitialized, then its gets an undefined value but if you have a local char variable should that not have an undefined value as well? Of course 0 could be that undefined value, but i was wondering if char is any different, since all related info i find is about int and the program below just outputs 0 when the char variable is cast to an int. Im using GCC 4.7 with no flags.

int main()
{
char test1;
int test2;

std::cout<<test2; //garbage
std::cout<<std::endl;
std::cout<<(int)test1; //0
    return 0;
}


推荐答案

未初始化意味着未初始化。仅仅因为你在特定的时间对你的机器持续有特殊的价值,并不意味着所有机器都会一直如此。

Uninitialised means really uninitialised. Just because you consistently get a particular value on your machine at a particular time, doesn't mean that will always be the case all the time on all machines.

您可以通过为您的函数转储汇编代码并检查它来验证没有任何内容正在初始化变量。

You can verify that nothing is initialising your variable by dumping the assembly code for your function and inspecting it.

这篇关于未初始化的本地字符的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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