简体   繁体   English

在对象初始化之前访问 C++ std::vector 对象成员

[英]Accessing C++ std::vector object members before object initialization

I just realized that I can access object members of my empty object vector list.我刚刚意识到我可以访问我的空对象向量列表的对象成员。 I thought vector.reserve(nmb) just reserves the required memory (kind of nmb*sizeof(object)).我认为 vector.reserve(nmb) 只是保留所需的内存(一种 nmb*sizeof(object))。

#include <iostream>
#include <vector>



class Car {

    public: int tires = 4;

    Car(void) {
        std::cout << "Constructor of Car" << std::endl;
    }
};

int main()
{
    std::vector<Car> carList;
    carList.reserve(20);
    std::cout << "Car tires: " << carList[0].tires << std::endl;
    std::cout << "Now comes the emplace_back:" << std::endl;
    carList.emplace_back();
    std::cout << "Car tires: " << carList[0].tires << std::endl;

    //Car carArray[20];
    //std::cout << "Car tires: " << carArray[0].tires << std::endl;

    return 0;
}

gives me:给我:

Car tires: 0                                                                                                                                        
Now comes the emplace_back:                                                                                                                         
Constructor of Car                                                                                                                                  
Car tires: 4                                                                                                                                        

...Program finished with exit code 0 

Why can I access members of not yet initialized objects?为什么我可以访问尚未初始化的对象的成员? Thank you.谢谢你。

Because the operator[] does not do any boundary checks (and just translates to some pointer arithmetic when you compile an optimized binary).因为operator[]不进行任何边界检查(并且在编译优化的二进制文件时仅转换为一些指针算术)。 This is so std::vector can be as efficiently used as a plain C array.这样std::vector可以像普通的 C 数组一样有效地使用。

The 0 that you read is just what happens to be in memory where the vector has reserved space on the heap.您读取的0恰好在内存中,向量在堆上保留了空间。 Note that the operating system initializes the program's memory with 0 as a security measure (so you cannot peek on old data).请注意,操作系统将程序的内存初始化为0作为安全措施(因此您无法查看旧数据)。 If your applications runs a bit longer and pages are recycled, you can also observe other garbage values.如果您的应用程序运行时间更长并且页面被回收,您还可以观察其他垃圾值。

You could try at() instead which will check for boundaries.您可以尝试at()来检查边界。

Accessing elements of a std::vector outside its size (not its capacity) has undefined behavior .访问std::vector超出其大小(不是其容量)的元素具有未定义的行为

Undefined behavior means that there is no guarantee that anything specific will happen in general.未定义的行为意味着不能保证任何特定的事情都会发生。 What exactly happens will depend on the particular implementation of the compiler and the standard library.究竟发生了什么取决于编译器和标准库的特定实现。

One likely behavior was mentioned by @ypnos, but (any) other behavior would also be permissible by the C++ standard and possible depending on how exactly the standard library implements std::vector and how exactly the compiler optimizes. @ypnos 提到了一种可能的行为,但是 C++ 标准也允许(任何)其他行为,并且可能取决于标准库实现std::vector准确程度以及编译器优化的准确程度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM