What is the meaning of contiguous memory in C++?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It means that memory is allocated as a single chunk. This is most often used when talking about containers.
For instance, the
vectorandstringclasses use a contiguous chunk of memory. This means that if you have a vector that contains theintelements123,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).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.
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.