getting the size of dynamic array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdulsamad
    New Member
    • Feb 2008
    • 7

    getting the size of dynamic array

    Hello there
    i wanna get the the size of a dynamic array of integer type which i have created like that.........

    [CODE=cpp] int *arr=0;
    arr = createTempArray ();
    arr = storeRowValues( arr);
    arr = storeColValues( arr);
    check(arr);
    }
    }
    }
    int* createTempArray ()
    {
    int length = rowIndex+colInd ex;
    int *tempArray = new int[length];
    return tempArray;
    }
    int *storeRowValues (int *tempArr)
    {
    for(int i=0;i < colIndex; ++i)
    {
    tempArr[i]=array[rowIndex][i];
    }
    return tempArr;
    }
    int *storeColValues (int *tempArr)
    {
    int size = sizeof(tempArr) / sizeof(tempArr[0]);

    for(int i=0; i < size; i++)
    { if(tempArr[i]==7)
    {
    --i;
    break;
    }
    }[/CODE]


    I wanna get the the value of size in

    storeColValues( int *tempArr) method ..............

    **********code* **********
    [CODE=cpp]int size = sizeof(tempArr) / sizeof(tempArr[0]);[/CODE]

    this line always gives me a result of 1



    somebody please help me and give me some hint or piece of code so that i can find the size correctly...... ..
    Last edited by Ganon11; Feb 13 '08, 11:29 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    When you pass an array (or pointer to an array) to a function, the function doesn't know for certain if it is an array or not - it just gets an address. It assumes this address is pointing to 1 - and only 1 - element. Thus, the sizeof(array) call returns the size of that pointer, not the size of the entire array.

    The only way for a function to know the size of an array is to pass the length as an argument, or use the length as a global constant variable.

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      In visual c++ you can use the _msize() to get the size of the dynamic array. This is not a standard c++ function so it is not portable. Other than that you are stuck with having to create a variable for the array size.

      Comment

      Working...