Menu

Array memory size

2002-12-31
2012-09-26
  • Nobody/Anonymous

    Hello,
        Is it poosible to determine array size in system memory and if so could someone point me in the right direction?

    Thanks
    Steve

     
    • Anonymous

      Anonymous - 2002-12-31

      The sizeof() operator returns the size of an array in bytes. Caution however: you must use an array name, not a pointer, otherwise you get the size of a pointer.

      char array[10] ;
      char* arrayptr = array ;

      size_t aa = sizeof( array) ;      // aa = 10
      size_t bb = sizeof( arrayptr) ;  // bb = sizeof(char*) = 4
      So if your array is passed to a function by pointer, you loose the size!

      Note that if you need to know the number of elements in an arry, you divide the size of the array by the size of a single element:

      int array[10] ;

      int elements = sizeof( array ) / sizeof( array[0] ) ;

      The line above works even when the type of array is changed.

       
    • Nobody/Anonymous

      Better use a vector if you can. Then you can change the size whenever you want and get it with int i = vectorname.size(); even when passed to a function etc.

      Jay

       
    • Nobody/Anonymous

      If you create an array of int's, and have say, 20 items.  Now lets say the size of an int on your computer is 4 bytes , now the memory for that array will be 80 bytes.

      N@N!

       
    • Nobody/Anonymous

      Thank you for your help. It provided exactly what I needed.

      Steve

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.