Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to get the size of an array

how to get the size of an array

Scheduled Pinned Locked Moved C / C++ / MFC
c++comdata-structurestutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Waleed Eissa
    wrote on last edited by
    #1

    hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
    www.wal2k.com

    C T W 3 Replies Last reply
    0
    • W Waleed Eissa

      hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
      www.wal2k.com

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      not in general. if you know the arrays are always allocated with malloc, calloc or realloc, you can use _msize to get the allocated size, then divide by the type size. but that's a giant hack and is very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) it's best to just pass the array size, or use a dynamic container like std::vector. Cleek | Image Toolkits | Thumbnail maker

      B J 2 Replies Last reply
      0
      • C Chris Losinger

        not in general. if you know the arrays are always allocated with malloc, calloc or realloc, you can use _msize to get the allocated size, then divide by the type size. but that's a giant hack and is very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) it's best to just pass the array size, or use a dynamic container like std::vector. Cleek | Image Toolkits | Thumbnail maker

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Chris Losinger wrote: but that's a giant hack and is an very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) For that reason, I never could get the infamous countof to work!

        // the countof macro for calculating array size
        #define countof(X) (sizeof(X) / sizeof((X)[0]))

        Good to know why it doesn't work.. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        J 1 Reply Last reply
        0
        • C Chris Losinger

          not in general. if you know the arrays are always allocated with malloc, calloc or realloc, you can use _msize to get the allocated size, then divide by the type size. but that's a giant hack and is very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) it's best to just pass the array size, or use a dynamic container like std::vector. Cleek | Image Toolkits | Thumbnail maker

          J Offline
          J Offline
          Jose Lamas Rios
          wrote on last edited by
          #4

          Chris Losinger wrote: it's best to just pass the array size, or use a dynamic container like std::vector. True. Another alternative, which is used sometimes for arrays of pointers, are NULL terminated arrays. -- jlr http://jlamas.blogspot.com/[^]

          1 Reply Last reply
          0
          • B Bob Stanneveld

            Chris Losinger wrote: but that's a giant hack and is an very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) For that reason, I never could get the infamous countof to work!

            // the countof macro for calculating array size
            #define countof(X) (sizeof(X) / sizeof((X)[0]))

            Good to know why it doesn't work.. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

            J Offline
            J Offline
            Jose Lamas Rios
            wrote on last edited by
            #5

            That macro only works if the compiler can see X as an array:

            #define COUNT_OF(X) (sizeof(X) / sizeof((X)[0]))
             
            void DisplayNumbers(int[6] numbers);
            void DisplayNumbers2(int* pNumbers);
            void DisplayNumbers3(int* pNumbers, int nSize);
             
            void main()
            {
              int numbers[] = {1, 2, 3, 5, 8, 13};
             
              // Here it works fine
              int nSize = COUNT_OF(numbers);
             
              DisplayNumbers(numbers);
             
              DisplayNumbers2(numbers);
             
              DisplayNumbers3(numbers, nSize);
            }
             
            void DisplayNumbers(int[6] numbers)
            {
              printf("DisplayNumbers shows:\n ");
             
              // Here it works fine too, although it's
              // not very useful because you are already
              // stating the size is 6
              int nSize = COUNT_OF(numbers);
              for (int i = 0; i < nSize; ++i)
              printf("%d ", numbers[i]);
             
              printf("\n");
            }
             
            void DisplayNumbers2(int* pNumbers)
            {
              printf("DisplayNumbers2 shows:\n ");
             
              // Here it doesn't work at all, because for
              // all the compiler knows, pNumbers is just
              // a pointer to an int. Thus, sizeof(X) in
              // the macro will return 4 (i.e., the size
              // of any pointer), sizeof((X)[0]) will
              // return 4 too, and the result for COUNT_OF(X)
              // will then be 1.
              int nSize = COUNT_OF(pNumbers); // will return 1
              for (int i = 0; i < nSize; ++i)
              printf("%d ", numbers[i]);
             
              printf("\n");
            }
             
            void DisplayNumbers3(int* pNumbers, int nSize)
            {
              printf("DisplayNumbers3 shows:\n ");
             
              for (int i = 0; i < nSize; ++i)
              printf("%d ", numbers[i]);
             
              printf("\n");
            }

            The output for this program would be:

            DisplayNumbers shows:
              1 2 3 5 8 13
            DisplayNumbers2 shows:
              1
            DisplayNumbers3 shows:
              1 2 3 5 8 13

            -- jlr http://jlamas.blogspot.com/[^]

            1 Reply Last reply
            0
            • W Waleed Eissa

              hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
              www.wal2k.com

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              Waleed wrote: is there anyway in C++ that I could get the size of a simple type array? no, but you might know the size as you explicitely told the amount of memory needed to build it...


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              1 Reply Last reply
              0
              • W Waleed Eissa

                hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
                www.wal2k.com

                W Offline
                W Offline
                Waleed Eissa
                wrote on last edited by
                #7

                thanks to all of you, I was thinking about using a special value to end my array as it's posted here...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups