简体   繁体   中英

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. 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).

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. 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.

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. 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.

If you write the following statement

int arr[3];

then you're reserving 3 contiguous memory cells of type integers. So if we say that integer reserves 4 bytes in memory, and the address of the first memory cell is 1000, then

The address of arr[0] in memory is 1000

The address of arr[1] in memory is 1004

The address of arr[2] in memory is 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. For example

The address of a in memory is 1000

The address of b in memory is 2014

The address of c in memory is 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++. One simple way is arrays as in C

int a[10]

STL containers like std::vector and std::array (C++11) would also allocate contiguous memory.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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