static array vs dynamic  : memory allocation
Difference between en1 and en2, changed 9 character(s)


**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<array>  ↵
 #include<vector> ↵
 ... ↵
 auto myArray =array<int,4>{1,2,3,4} ;  ↵
 auto myVector =vector<int>{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 
is actualy is a pointer (located in the stack)  assocaited to data located in the heap ↵









 

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English marouenkd_999 2025-08-13 19:12:28 9
en1 English marouenkd_999 2025-08-13 19:11:18 1337 Initial revision (published)