static array vs dynamic : memory allocation

Revision en1, by marouenkd_999, 2025-08-13 19:11:18

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 is actualy a pointer (located in the stack) assocaited to data located in the heap

Tags stack memory, vector, pointer

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)