c++ - 了解我有多少内存可用于动态 vector C++

标签 c++ memory vector

<分区>

我是 C++ 编程的新手;在我的程序中,我必须在从 .csv 文件读取后动态分配一个 vector vector 和两个带有 std::vector 的 vector 。如果矩阵可以在堆中分配我必须使用不同的函数。但我不知道从分配的角度来看这是如何表现的。

最佳答案

std::vector 是 C++ 标准库提供的“用户自定义类型”。它充当一个容器,动态保存其数据。这就是它可以随着 push_back() 增长的原因。

让我们检查一个非常简化的 std::vector 版本: 从此类实例化的对象将仅包含整数作为数据,并且必须在创建时指定大小

class VectorOfInts {

private:
    int* data; // pointer to where all of the data is stored on heap
    int sz;

public:
    VectorOfInts(int size) :data{new int[size]}, sz{size}  
    {
        for (int i=0; i<sz; ++i) data[i]=0; //initialize all ints to 0 
    }

    ˜VectorOfInts() { delete[] data; } //when the VectorOfInts is destroyed, 
    //we have to remember to release all of our data.

    int getSize() const
    {
        return sz;
    }

    void setValue(int index, const int& value)
    {
        if(index>=sz || index<0) 
            {
                std::cout<<"invalid index\n";
                return;
            }
        //since value is type int which is an integral type, it is copied
        //if we had a vector of struct or class instead, 
        //copy constructor (or suitable move constructor if defined)
        //would have been called. 
        //this is the most crucial part to understand about this code.
        data[index] = value;  
    }
};

如您所见,数据由 VectorOfInts 在免费商店中动态地进行内部管理。事实上,无论您通过构造函数创建的容器有多大,类对象本身的大小都是相同的。这是因为您的 VectorOfInts 本身只有一个指针和一个 int

每次设置值时,都必须将参数复制到由 VectorOfInts 分配的动态内存中。对于 int 这没什么大不了的,但是如果我们有一个大对象的 vector (有自己的指针和对象作为字段)而不是 int,那将是可取的定义合适的移动构造函数或考虑保留指针 vector 而不是实际对象。

这就是 std::vector 工作原理的本质。有一些差异,包括以下内容:

1- std::vector 可以随大小增长和收缩:std::vector 将跟踪总容量,而不是单独的大小如果 vector 增长,分配和可用的自由存储的大小,以及“大小”,这是 vector 持有的元素的实际数量。如果大小超过容量, vector 将在堆中重新分配更大的内存块,复制所有旧数据,并释放旧内存位置。

2- std::vector 允许通用类型内容:这对于实现来说不是问题,因为 vector 将为对象类型的大小 * 元素的数量分配内存。这保证有效,因为每个 vector 在其生命周期内只包含特定类型的对象。

我现在将提供一些示例来准确显示处理 std::vector 时内存的去向:

#include <iostream>
#include <vector>

void  helper()
{
    std::vector<int> A; //vector created on the stack
    int a=5;
    int b=6;
    A.push_back(a); //a is copied to another place in memory allocated by A
    A.push_back(b); //same thing with b, they are both copied; 

    std::vector<int>* B = new std::vector<int>(); //B is a pointer only
    //B occupies as much space as any pointer on your machine, but what it points to
    //is a dynamically allocated block of memory that holds a vector
    B->push_back(a); //a is copied to free store
    B->push_back(b); //same thing with b;

    free(B); //this is extremely important, 
             //since B is allocated on the heap, it must be freed
             //or else we will have a memory leak.  
             //the destructor for std::vector is smart
             //it will in turn free its own dynamically allocated memory upon destruction.

} //when this function returns, a, b and A are all destroyed
  // because they fall off the stack
  //A's destructor is called implicitly and the block of memory
  // that is allocated by A is free's by its destructor

在主函数中我们有:

int main() {


    helper(); 

    std::vector<int> A;  //created on the stack; 
    A.push_back(1); //the value 1 is copied to dynamic memory by A
    A.push_back(2);
    A.push_back(3);

    std::vector<int> B;  //created on the stack; 
    B.push_back(4); //the value 4 is copied to dynamic memory by B
    B.push_back(5);
    B.push_back(6);

    std::vector<std::vector<int>> C;  //also created on the stack

    C.push_back(A);  //the content of A (including its internal pointers) 
                     //is copied to an area of heap allocated by C
    C.push_back(B);  //same for B, notice that A and B are copied 



    return 0;
} //destructor for A, B and C are called. 

关于c++ - 了解我有多少内存可用于动态 vector C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34371818/

相关文章:

c++ - 如何在mac中编辑/usr/include/dlfcn.h

c++ - Boost Spirit 换行和输入结束

c++ - 任务管理器显示的线程比我创建的多

数组大小为零时的 C++ 内存分配行为

c++ - 平台改变后全局变量不稳定

r - 在 R 中的两个向量列表之间执行运算

c++ - 不能 push_back 到 vector - 没有给出过载错误

C++ - 函数指针和类

c++ - wxWidgets 有某种垃圾收集器吗?

C++ vector 读取访问冲突 Mylast 返回 0x8