Getting an array length in C# is a trivial task. All we need to so is call the Length property of the Array Class:

int[] arr = new int[17];
int arrLength = arr.Length;

 

Getting an array length in C++ might be less trivial:

int arr[17];
int arrSize = sizeof(arr) / sizeof(int);

Notice that sizeof(arr) returns the array size in bytes, not its length, so we must remember to divide its result with the size of the array item type (sizeof(int) in the example above).

I want to show you this nice C++ macro which helps us getting an array length in another and better way:

template<typename T, int size>
int GetArrLength(T(&)[size]){return size;}

This is how to use it:

template<typename T, int size>
int GetArrLength(T(&)[size]){return size;}

void main()
{
    int arr[17];
    int arrSize = GetArrLength(arr);
}

aarSize will be equal to 17. Enjoy!

Tags :

16 Responses to “C++ Tip: How To Get Array Length”


  1. James Curran

    Said on January 12, 2009 :

    It’s interesting to note that shortly after that technique was devised, a somewhat anonomoused version:

    template
    int func(T(&)[N]){return N;}

    was passed around members of the C++ Standardization Committee, and it usually took quite a while for the recipient to figure out what it did.

  2. Steve Vinoski

    Said on January 13, 2009 :

    For an array named “arr” just do this:

    sizeof(arr)/sizeof(*arr)

    No need for templates here, no need to specify the item type in the divisor, and it works perfectly in both C++ and C.

  3. Troy

    Said on January 13, 2009 :

    Wow, neat trick, any chance you can explain how it works exactly?

  4. James Curran

    Said on January 14, 2009 :

    @troy:
    It’s fairly straight forward. The compiler uses parameter resolution to create a version of the function that matches that parameter passed to it, so :

    int arr[17];
    int arrSize = GetArrLength(arr);

    is essentially

    int arr[17];
    int arrSize = GetArrLength(arr);

    which creates this function:

    int GetArrLength(int(&)[17]){return 17;}

  5. James Curran

    Said on January 14, 2009 :

    The comment editor mangled that:

    “that is essentially:
    int arr[17];
    int arrSize = GetArrLength<int, 17>(arr);

  6. OJ

    Said on January 27, 2009 :

    Bear in mind that things aren’t so simple when dealing with dynamically allocated arrays.

  7. Kristof

    Said on February 19, 2009 :

    Don’t put it in a seperate function, because you will get this:

    int array[20];

    int GetArrayLength(int array[])
    {
    return sizeof array / sizeof array[0];
    }

    Because you only pass the first element of the array to the function, so this will always return 1 :)

  8. Cengiz

    Said on November 16, 2009 :

    First of all i must say that im new to c++.

    I wonder how can we determine the size of array in a function when we send the array to that function. (We already know the length of array in a function in which we creat it.)

    main() {
    int myArray[20];
    someFunction(myArray);
    }

    void someFunction(int anArray[]) {
    // How can we determine the size here?
    }

  9. elham

    Said on February 1, 2010 :

    how I get array size by user in visual studio c++?

  10. Prakash

    Said on March 10, 2010 :

    how to find Size of a class withot using sizeof operator

  11. amperstand in parenthesis?

    Said on April 9, 2010 :

    Could you explain the (&) operator?

  12. Anonymous

    Said on July 28, 2010 :

    Good article.
    There is only one beef I have with it.
    void main is not standard. Refrain from using this. Use int main.

  13. rakesh

    Said on September 14, 2010 :

    how to find the size of dynamically allocated array using VC++ 2008

  14. ccccc

    Said on June 27, 2011 :

    u can get easy array size without sizeof only wat u have todo use ur brain..

    easiest way write to forum and ask for help or correct ur code coz u to dump to correct it by ur self.. in future will be many programmers who eeven dont undesnatnfd wat they doing…

  15. lokesh

    Said on August 10, 2011 :

    Hi All,

    If we pass an array to a function , then how to find its lenght ?
    Example:
    int main(){
    string arr[4]={“Ravi”,”Kavi”,”Kajal”,”Sonu”};
    functiontoDosomeOperation(arr);
    }
    // Here functiontoDosomeOperation will do some operation and need //to know lenght of arr.

    void functiontoDosomeOperation(string arr[])
    {

    int lenght = ?
    // If we use GetArrLength template funtion then it gives compilation error.

    error: no matching function for call to `GetArrLength(std::string*&)’

    and if we use sizeof operator then it returns 1 as in function arr is pointer

  16. jessica

    Said on April 17, 2012 :

    simply store the size of the array when you first declare it and pass the size to functions. e.g.

    int main()
    {
    const int bufferSize = 256;
    char buffer[bufferSize];

    zero(buffer, bufferSize);
    return 0;
    }

    ———————-

    zero(const char *buffer, const int size)
    {
    for (int i = 0; i < size; ++i)
    buffer[i] = 0;
    }

Post a Comment