C++ 当程序进入主功能时,为什么要清除这个std::vector?

C++ 当程序进入主功能时,为什么要清除这个std::vector?,c++,vector,static,initialization,C++,Vector,Static,Initialization,我遇到的问题是,当所有静态变量都初始化时,我在初始化时将一个元素推入向量,然后当它进入主函数时,向量重置为0,就好像它已清除一样。在我的例子中,向量位于不同的CPP中,但这也显示了问题: #include <iostream> #include <vector> struct List { static std::vector<int> listVector; }; struct Foo { Foo() { std

我遇到的问题是,当所有静态变量都初始化时,我在初始化时将一个元素推入向量,然后当它进入主函数时,向量重置为0,就好像它已清除一样。在我的例子中,向量位于不同的CPP中,但这也显示了问题:

#include <iostream>
#include <vector>

struct List
{
    static std::vector<int> listVector;
};

struct Foo
{
    Foo() 
    {
        std::cout << List::listVector.size() << '\n';   // Prints 0
        List::listVector.push_back(1);
        std::cout << List::listVector.size() << '\n';   // Prints 1
    }
};
Foo fooObj;

std::vector<int> List::listVector;

int main()
{
    std::cout << List::listVector.size() << '\n';   // Prints 0
    List::listVector.push_back(2);
    std::cout << List::listVector.size() << '\n';   // Prints 1
}
#包括
#包括
结构列表
{
静态std::向量listVector;
};
结构Foo
{
Foo()
{

std::cout您必须了解,您的程序不是自上而下运行的。每个程序都将从main()函数开始。如果main()函数中没有调用main()上面的所有代码,那么它基本上会被忽略


但是,这里不应该问这样的问题。看起来你是个初学者,在提问之前,你应该先阅读更多关于这个主题的内容。

你必须明白,你的程序不是自上而下运行的。每个程序都会从main()函数开始。main()上面的所有代码如果在main()函数中没有调用它,它基本上会被忽略


但是,这里不应该问这样的问题。你似乎是一个初学者,在提问之前,你应该先阅读更多关于这个主题的内容。

如果你将代码更改为

#include <iostream>
#include <vector>

namespace List
{
    static std::vector<int> listVector;
}

struct Foo
{
    Foo()
    {
        std::cout << List::listVector.size() << '\n';   // Prints 0
        List::listVector.push_back(1);
        std::cout << List::listVector.size() << '\n';   // Prints 1
    }
};
Foo fooObj;

//std::vector<int> List::listVector;

int main()
{
    std::cout << List::listVector.size() << '\n';   // Prints 0
    List::listVector.push_back(2);
    std::cout << List::listVector.size() << '\n';   // Prints 1
}
#包括
#包括
名称空间列表
{
静态std::向量listVector;
}
结构Foo
{
Foo()
{

如果您将代码更改为

#include <iostream>
#include <vector>

namespace List
{
    static std::vector<int> listVector;
}

struct Foo
{
    Foo()
    {
        std::cout << List::listVector.size() << '\n';   // Prints 0
        List::listVector.push_back(1);
        std::cout << List::listVector.size() << '\n';   // Prints 1
    }
};
Foo fooObj;

//std::vector<int> List::listVector;

int main()
{
    std::cout << List::listVector.size() << '\n';   // Prints 0
    List::listVector.push_back(2);
    std::cout << List::listVector.size() << '\n';   // Prints 1
}
#包括
#包括
名称空间列表
{
静态std::向量listVector;
}
结构Foo
{
Foo()
{
标准::cout
我很想知道,当Foo构造函数运行时,它会向向量中添加一个元素,但是向量还没有创建,因为它是在Foo对象创建之后写在行上的

#include <iostream>
#include <vector>

int main()
{
    // initialize a block of memory to zero:
    alignas(std::vector<int>) char memory[sizeof(std::vector<int>)] = {};
    // use that memory as a vector, even though we haven't created any vector
    // (this is undefined behaviour! there is no vector yet!):
    std::vector<int>& vec = *reinterpret_cast<std::vector<int>*>(memory);
    vec.push_back(1);
    // the non-existent "phantom vector" has size 1:
    std::cout << vec.size() << std::endl;
    // now use "placement new" to construct an empty vector in that memory:
    new (memory) std::vector<int>();
    // the real vector is empty, the element in the phantom vector is lost:
    std::cout << vec.size() << std::endl;    
}
是的,完全正确。
fooObj
构造函数只访问要构造向量的内存,但它还没有在那里构造。(因此程序的行为未定义,必须修复)

然而,如果是这种情况,那么当我在构造函数中这样做并且它打印出一个大小为1的向量时,我要添加什么向量呢

编译器只是将内存解释为一个有效的向量,因为它无法知道其他情况。这恰好起作用,因为将在其中构造全局对象的内存最初为零,因为执行环境确保所有静态对象的内存最初为零,这恰好与defau相同lt构造状态(用于实现对
std::vector
的定义)。程序无法知道包含所有零的内存位置不是有效的向量

稍后,向量的构造函数将运行并重新初始化内存,使其处于默认构造状态

下面是一个示例,展示了您的程序的功能:将包含所有零的原始内存解释为一个向量,并将元素添加到该虚拟向量(它实际上不存在),然后在该内存中实际构建一个向量。一旦创建了实向量,添加到虚拟向量的任何内容都将丢失

#include <iostream>
#include <vector>

int main()
{
    // initialize a block of memory to zero:
    alignas(std::vector<int>) char memory[sizeof(std::vector<int>)] = {};
    // use that memory as a vector, even though we haven't created any vector
    // (this is undefined behaviour! there is no vector yet!):
    std::vector<int>& vec = *reinterpret_cast<std::vector<int>*>(memory);
    vec.push_back(1);
    // the non-existent "phantom vector" has size 1:
    std::cout << vec.size() << std::endl;
    // now use "placement new" to construct an empty vector in that memory:
    new (memory) std::vector<int>();
    // the real vector is empty, the element in the phantom vector is lost:
    std::cout << vec.size() << std::endl;    
}
#包括
#包括
int main()
{
//将内存块初始化为零:
alignas(std::vector)字符内存[sizeof(std::vector)]={};
//将该内存用作向量,即使我们尚未创建任何向量
//(这是未定义的行为!还没有向量!):
std::vector&vec=*重新解释(内存);
向量推回(1);
//不存在的“幻影向量”大小为1:
标准::cout
我很想知道,当Foo构造函数运行时,它会向向量中添加一个元素,但是向量还没有创建,因为它是在Foo对象创建之后写在行上的

#include <iostream>
#include <vector>

int main()
{
    // initialize a block of memory to zero:
    alignas(std::vector<int>) char memory[sizeof(std::vector<int>)] = {};
    // use that memory as a vector, even though we haven't created any vector
    // (this is undefined behaviour! there is no vector yet!):
    std::vector<int>& vec = *reinterpret_cast<std::vector<int>*>(memory);
    vec.push_back(1);
    // the non-existent "phantom vector" has size 1:
    std::cout << vec.size() << std::endl;
    // now use "placement new" to construct an empty vector in that memory:
    new (memory) std::vector<int>();
    // the real vector is empty, the element in the phantom vector is lost:
    std::cout << vec.size() << std::endl;    
}
是的,完全正确。
fooObj
构造函数只访问要构造向量的内存,但它还没有在那里构造。(因此程序的行为未定义,必须修复)

然而,如果是这种情况,那么当我在构造函数中这样做并且它打印出一个大小为1的向量时,我要添加什么向量呢

编译器只是将内存解释为一个有效的向量,因为它无法知道其他情况。这恰好起作用,因为将在其中构造全局对象的内存最初为零,因为执行环境确保所有静态对象的内存最初为零,这恰好与defau相同lt构造状态(用于实现对
std::vector
的定义)。程序无法知道包含所有零的内存位置不是有效的向量

稍后,向量的构造函数将运行并重新初始化内存,使其处于默认构造状态

下面是一个示例,展示了您的程序的功能:将包含所有零的原始内存解释为一个向量,并将元素添加到该虚拟向量(它实际上不存在),然后在该内存中实际构建一个向量。一旦创建了实向量,添加到虚拟向量的任何内容都将丢失

#include <iostream>
#include <vector>

int main()
{
    // initialize a block of memory to zero:
    alignas(std::vector<int>) char memory[sizeof(std::vector<int>)] = {};
    // use that memory as a vector, even though we haven't created any vector
    // (this is undefined behaviour! there is no vector yet!):
    std::vector<int>& vec = *reinterpret_cast<std::vector<int>*>(memory);
    vec.push_back(1);
    // the non-existent "phantom vector" has size 1:
    std::cout << vec.size() << std::endl;
    // now use "placement new" to construct an empty vector in that memory:
    new (memory) std::vector<int>();
    // the real vector is empty, the element in the phantom vector is lost:
    std::cout << vec.size() << std::endl;    
}
#包括
#包括
int main()
{
//将内存块初始化为零:
alignas(std::vector)字符内存[sizeof(std::vector)]={};
//将该内存用作向量,即使我们尚未创建任何向量
//(这是未定义的行为!还没有向量!):
std::vector&vec=*重新解释(内存);
向量推回(1);
//不存在的“幻影向量”大小为1:

std::没有时间找到标准链接,但是
fooObj
listVector
的初始化顺序是可疑的。用您自己的类替换该向量,并向构造函数添加打印或其他内容。要清楚,这应该与静态初始化顺序失败不同,因为它是同一个翻译单元.我相信他们的姓名缩写是正确的