简体   繁体   English

C ++中连续内存的含义是什么?

[英]What is the meaning of contiguous memory in C++?

C ++中连续内存的含义是什么?

It means that memory is allocated as a single chunk. 这意味着内存被分配为单个块。 This is most often used when talking about containers. 在谈论容器时最常使用这种方法。

For instance, the vector and string classes use a contiguous chunk of memory. 例如, vectorstring类使用连续的内存块。 This means that if you have a vector that contains the int elements 123 , 456 , 789 , then you can be assured that if you get the pointer to the first element of the vector, by incrementing this pointer, you'll access the second element (456), and by incrementing it again you'll access the last element (789). 这意味着,如果你有一个包含一个向量int元素123456789 ,那么你可以放心,如果你的指针向量的第一个元素,通过增加此指针,则可以直接进入第二个元素(456),再次递增它,你将访问最后一个元素(789)。

std::vector<int> vec = {123, 456, 789};

int* ptr = &vec[0];
*ptr++ == 123; // is true
*ptr++ == 456; // is true
*ptr++ == 789; // is true

The deque class, on the other hand, does not guarantee contiguous storage. 另一方面,deque类不保证连续存储。 This means that if you have a deque that contains the same elements (123, 456, 789), and that you get a pointer to the first element, you cannot be certain that you'll access the second element by incrementing the pointer, or the third by incrementing it again. 这意味着如果你有一个包含相同元素(123,456,789)的双端队列,并且你得到一个指向第一个元素的指针,你就不能确定你是通过递增指针来访问第二个元素,或者第三个是再次递增它。

std::deque<int> deque = {123, 456, 789};

int* ptr = &deque[0];
*ptr++ == 132; // true
*ptr++ == 456; // not necessarily true and potentially dangerous
*ptr++ == 789; // not necessarily true and potentially dangerous

Another example of a non-contiguous data structure would be the linked list. 非连续数据结构的另一个例子是链表。 With a linked list, it's almost unthinkable that incrementing the head pointer could return the second element. 使用链表,增加头指针可能会返回第二个元素几乎是不可想象的。

It is rarely relevant, assuming you use C++ good practices such as using iterators instead of pointers as much as you can, because it lets collections manage how they store their items without having to worry about how they do it. 假设您使用C ++良好实践(例如尽可能多地使用迭代器而不是指针),这很少相关,因为它可以让集合管理它们存储项目的方式,而不必担心它们是如何操作的。 Usually, you'll need memory to be contiguous if you have to call C code from your C++ code, as most C functions were designed to work with contiguous memory because that's the simplest way to do it. 通常,如果必须从C ++代码调用C代码,则需要内存连续,因为大多数C函数都设计为使用连续内存,因为这是最简单的方法。

If you write the following statement 如果你写下面的陈述

int arr[3];

then you're reserving 3 contiguous memory cells of type integers. 然后你要保留3个整数类型的连续存储单元。 So if we say that integer reserves 4 bytes in memory, and the address of the first memory cell is 1000, then 因此,如果我们说整数在内存中保留4个字节,并且第一个存储单元的地址是1000,那么

The address of arr[0] in memory is 1000 内存中arr [0]的地址为1000

The address of arr[1] in memory is 1004 内存中arr [1]的地址是1004

The address of arr[2] in memory is 1008 内存中arr [2]的地址是1008

They are contiguous, one after another. 它们是一个接一个的连续。 But if you just write 但如果你只是写

int a,b,c;

you're reserving non-contiguous memory cells. 你正在保留非连续的存储单元。 So the address of "a" may be far away from b and c. 所以“a”的地址可能远离b和c。 For example 例如

The address of a in memory is 1000 内存中的地址是1000

The address of b in memory is 2014 内存中b的地址是2014

The address of c in memory is 2234 内存中c的地址是2234

They are not contiguous. 它们不是连续的。 That's all 就这样

First of all contiguous memory means a chunk of memory allocated without any gaps in the addresses it occupies. 首先,连续内存意味着分配的内存块在其占用的地址中没有任何间隙。 It will be one single "block" of memory. 它将是一个单独的“块”内存。

Contiguous memory in C++ would mean various ways of allocating contiguous memory in C++. C ++中的连续内存意味着在C ++中分配连续内存的各种方式。 One simple way is arrays as in C 一种简单的方法是C中的数组

int a[10]

STL containers like std::vector and std::array (C++11) would also allocate contiguous memory. std::vectorstd::array (C ++ 11)这样的STL容器也会分配连续的内存。

The meaning of Contiguous Memory is continuous memory. 连续记忆的意义是连续记忆。 When Big Block of memory is reserved or allocated then that memory block is called as Contiguous Memory Block. 当保留或分配大块内存时,该内存块称为连续内存块。

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

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