marouenkd_999's blog

By marouenkd_999, history, 8 months ago, In English

Reminder : memory is divided in two main section heap and stack
- Heap for dynamic memory allocation - stack for static memory allocation

array vs vector in memory ?

we take this example

#include
#include ... auto myArray =array<int,4>{1,2,3,4} ;
auto myVector =vector{1,2,3,4} ; // Display the adress of myArray and myVector std::cout<<"adress of myArray " <<&myArray<<std::endl; std::cout<<"adress of myVector" <<&myVector<<std::endl ;
// display the adress the first adress of myArray and myVector std::cout<<"adress of myArray[0] " <<&myArray[0]<<std::endl; std::cout<<"adress of myVector[0]" <<&myVector[0]<<std::endl ;

Result :

adress of myArray 0055FBBC
  adress of myVector0055FBA4
  adress of myArray[0] 0055FBBC
  adress of myVector[0]00980C60

===> This show that c++ handle Vectors and array in a different way the adress of myArray is the same as the adress of the first element :the array is directly located where the data is stored (stack)

the adress of myVector is not the same as the adress of the first element: the vector actualy is a pointer (located in the stack) assocaited to data located in the heap

Full text and comments »

  • Vote: I like it
  • -10
  • Vote: I do not like it