c++ - 局部变量初始化的小问题
黄舟
黄舟 2017-04-17 13:30:56
0
2
849

  局部未初始化内置类型变量在函数中一般是默认初始化(相对于全局变量),默认初始化的值都是未知值,但是g++下main函数中的这种类型变量会被初始化为0或空(如果是string或者是指针的话),但是vs下main函数中的这种类型变量会初始化为未知值,这是跟编译器有关的吗?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
刘奇

As far as C language is concerned, local variables generally exist on the stack; unless it is some leaf function, a small number of local variables are placed directly in registers. The value in the stack is, of course, unknown by default. If the compiler generates a piece of code that specifically initializes local variables on the stack before referencing them, then it is similar to g++'s approach; otherwise, it is VS's approach.

Ty80

It should be yes, vs compiler, local built-in type variables are not initialized. C++ pursues higher performance. If it is not necessary, the C++ compiler will generally not do extra work, such as default initialization of variables. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!