Memory Usage Analysis of vector dynamic two-dimensional array (container)

Source: Internet
Author: User

I have previously written an article "dynamic two-dimensional array in C ++ ". There is no dynamic two-dimensional (multidimensional) array in C ++. However, we can create one by ourselves based on the principle.

After reading the source code of STL vector, "STL source code analysis" --- stl_vector.h Reading Notes ", I thought of using the container as a two-dimensional array.

Create a 2x4 2D array. The thought is: first create a container, the outer size of 2 (2 rows), and then the container size is 4 (4 columns ).

Int ROW = 2, Col = 4; vector <int> V (ROW); // V is the container's container, and the size is 2.for( int I = 0; I <row; I ++) V [I]. resize (COL); // set the container size of the layer to 4.

So how does V use the memory?

I know that the vector uses continuous memory, but the element inside the vector is also a vector. The internal vector size is unknown and variable, and the internal vector size may be different. However, vector requires that the elements occupy the same space. Therefore, the elements stored in the internal vector must not occupy the space of the external vector v.

After analyzing the source code of the vector, we can know that the vector has only three member variables:

iterator start;  iterator finish;  iterator end_of_storage;  
These three are all original ecological pointers, occupying 12 bytes on 32-bit machines. Therefore, each internal element vector occupies only 12 bytes, and the elements stored in the internal element vector open up memory elsewhere. Write code to verify:

Vector <vector <int> >:: iterator iter1 = v. begin (); vector <int>: iterator iter2 = iter1 + 1; cout <"distance (difference) between two iterators" <(INT) iter2-(INT) iter1 <Endl; cout <"memory size occupied by vector:" <sizeof (vector <int>) <Endl;
Output:

Distance (difference value) between two iterators 12
Memory occupied by vector: 12

The above statement is verified. The memory of the internal element vector is continuous in other places, that is to say, the memory occupied by each row of elements is continuous. Then write the code to verify it:

For (INT I = 0; I <row; I ++) // value of the initialization element for (Int J = 0; j <Col; j ++) V [I] [J] = I * Col + J + 1; for (INT I = 0; I <row; I ++) // output its address and value {<span style = "white-space: pre"> </span> for (Int J = 0; j <Col; j ++) <span style = "white-space: pre"> </span> cout <& V [I] [J] <": "<V [I] [J] <"; cout <Endl ;}
Output:

0x0012bb60: 1 0x0012bb64: 2 0x0012bb68: 3 0x0012bb6c: 4
0x0012bc90: 5 0x0012bc94: 6 0x0012bc98: 7 0x0012bc9c: 8

The memory of visible rows is continuous.

Therefore, it is concluded that:

A dynamic two-dimensional array constructed by vector <int> V. The internal vector occupies only 12 bytes (32-bit machines ). From its source code, we can see that all vectors only need three member variables. Elements in the vector are stored in the dynamically opened memory.

The following is the test code. The vector used is the STL of SGI version. The memory usage of the vector of other versions may be different. I tried it with vs2010 and it took 20 bytes.

# Include <iostream> # include <fstream> # include "vector" using namespace STD; int main () {ofstream fout ("result.txt"); int ROW = 2, col = 4; vector <int> V (ROW); // V is the container, and the size is 2.for( int I = 0; I <row; I ++) V [I]. resize (COL); // The container size in the layer is set to 4 vector <int> >:: iterator iter1 = v. begin (); vector <int>: iterator iter2 = iter1 + 1; fout <"distance (difference) between two iterators" <(INT) iter2-(INT) iter1 <Endl; fout <"memory size occupied by vector:" <sizeof (vector <int>) <Endl; For (INT I = 0; I <row; I ++) // initialization element value for (Int J = 0; j <Col; j ++) V [I] [J] = I * Col + J + 1; for (INT I = 0; I <row; I ++) // output its address and value {for (Int J = 0; j <Col; j ++) fout <& V [I] [J] <": "<V [I] [J] <"; fout <Endl;} return 0 ;}

Only the final result is output to result.txt, which is easy to copy.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.