Declaring an array with size in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SUNIL TYATA
    New Member
    • Feb 2010
    • 18

    #1

    Declaring an array with size in C++

    The java code below is well functioning code where I did was;
    I used nums.length as the size for the declaring the new arrary within the function body.

    Code:
    public int[] reverse3(int[] nums) 
    {
     int[] temp = new int[[B]nums.length[/B]];
     for(int i=0;i<nums.length;i++)
     {
      temp[i] = nums[nums.length-(i+1)];
     }   
     return temp;
    }
    If I am trying to do the same thing on c++ platform it doesn't work. Most of the time while creating array in c++ needs the constant size of the array.

    So I need the reasonable solution which declare the new array within the function which will have the size of the array passed through the function as parameter.

    eg.
    Code:
    function(int a[],int [B]len[/B])
    {
    int arr[[B]len[/B]];
    //while compiling this code will give error because size of arr should be constant as error comment of the compiler
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    C/C++ doesn't have dynamically sizeable arrays (well C99 does but not many people use that) so yes the size in an array declaration like that has to be constant. You need to dynamically allocate the array using new

    int* temp = new int[len];

    but remember that somewhere you code will have to delete the array with

    delete[] temp;

    or you will get a memory leak.

    Comment

    • SUNIL TYATA
      New Member
      • Feb 2010
      • 18

      #3
      I knew that It does work like nums.length work as a value for the size of the array in java but I need the code for the c++. Thank You though.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Sorry it is not clear which bit you still don't understand or require information about.

        Comment

        • icio
          New Member
          • Apr 2010
          • 1

          #5
          I believe that his problem is he would like to know how to work out the number of elements within an array.

          The `sizeof` function will tell you the number of bytes used to store the item that you pass as a parameter. I think I am correct in saying that you can use the following:
          Code:
          int* temp = new int[sizeof(nums) / sizeof(int)];
          Hope that helps.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You can use that but only on an array not on a pointer

            Code:
            int array[5];
            
            int size = sizeof array/ sizeof array[0];
            // At this point size = 5
            // but
            
            int* ptr = array;
            
            size = sizeof ptr/ sizeof ptr[0];
            
            // At this point size = 1 (or 2 on a 64 bit system)
            sizeof operates on the object provided, it does no deferencing so sizeof ptr returns the size of a int pointer or 4 on a 32bit system, 8 on a 64 bit system normally. sizeof ptr[0] provides the sizeof int which is the type ptr[0] has so it returns 4 (int has a size of 4 on most 32 and 64 bit systems.

            Using sizeof on a pointer tells you nothing of the array it points to (if indeed it does actually point to an array).

            Comment

            • SUNIL TYATA
              New Member
              • Feb 2010
              • 18

              #7
              Thank for reply but actuall I am talking about the array length not the size in bit or bytes.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                So was everyone else: The size (length) of a char array[8] is in this case 8
                i.e. sizeof(array) delivers 8 and sizeof(char) delivers 1. 8/1== 8.
                The size (length) of an int array[8] is in this case 8. i.e. sizeof(array) delivers 32
                and sizeof(int) delivers 4 (on a 32 bit system) so 32/4 == 8.

                Comment

                Working...