Menu

Calling Length() on items on the heap

Githlar
2004-09-23
2012-09-26
  • Githlar

    Githlar - 2004-09-23

    How would I go about calling Length() on items on the heap? I tried stuff like:
    pointer.length();
    pointer->length();
    pointer.length();
    pointer->length();
    &pointer.length()
    &pointer->length();

    As of now I am kind of stuck so please RSVP if possible

     
    • aditsu

      aditsu - 2004-09-23

      what type of pointer do you have, and what do you mean by length?

      Adrian

       
    • Githlar

      Githlar - 2004-09-23

      its a pointer to an char[5000] and isn't length a member function of all types of objects?

       
      • Nobody/Anonymous

        NO!

        you are thinking of java or javascript or actionscript or even ecmascript.

        native types in c/c++ are just that. native. they know nothing. they are not even smart enough to know about themselves.

        obag

         
    • Githlar

      Githlar - 2004-09-23

      In my C++ book its shows that you can call .length()...

       
    • Githlar

      Githlar - 2004-09-23

      So what would I use to find the length of an array?

       
      • joe

        joe - 2004-09-23

        try the following to see...

        include <iostream>

        using namespace std;
        int main()
        {
        int array[10];
        cout << sizeof(array) << endl
        << sizeof(array[0]) << endl
        << sizeof(array) / sizeof(array[0]) << endl;

        cin.get();
        return 0;
        

        }

        It will work on either heap or stack...

        This will print
        1)total bytes in array
        2)bytes per array element
        3)array elements in the array

        HTH

         
    • Githlar

      Githlar - 2004-09-23

      not the memory size, but the number of elements

       
    • Githlar

      Githlar - 2004-09-23

      aha! thank you very much!

       
    • joe

      joe - 2004-09-23

      Sorry...I misspoke...if you do it on the heap like this, it doesn't work...as the sizeof() a pointer is not the same as as the sizeof() an array.

      However...since it's on the heap, you must have called new at some point, right??

      Therefore you have all the info you need...

      include <iostream>

      using namespace std;
      int main()
      {
      int size = 10;
      int *array = new int[size];

      cout &lt;&lt; size * sizeof(array[0]) &lt;&lt; endl 
           &lt;&lt; sizeof(array[0]) &lt;&lt; endl
           &lt;&lt; size &lt;&lt; endl;
      
      delete [] array;
      cin.get();
      return 0;
      

      }

       
    • qWake

      qWake - 2004-09-23

      Be careful though, sizeof will not ALWAYS give the size of the array. If I recall the rule correctly, the array variable must be a non-parameter for sizeof to do what you want, otherwise it will just return the size of the pointer itself. This means that if you pass the array to a function, the function cannot get the size of the array itself since all it has is a pointer to something; in this case, your function needs to have another parameter that gives the array size.

      qWake

       
      • joe

        joe - 2004-09-23

        >>Be careful though, sizeof will not ALWAYS give the size of the array. If I recall the rule correctly, the array variable must be a non-parameter for sizeof to do what you want, otherwise it will just return the size of the pointer itself.<<

        good point...to the OP...what we are talking about is the following...it applies to my first example...on the stack. On the heap, you always have to do it the pointer way.

        include <iostream>

        using namespace std;

        void funcCalledIncorrectly(int* anArray){
        cout << "I'm in funcCalledIncorrectly" << endl;
        cout << sizeof(anArray) << endl
        << sizeof(anArray[0]) << endl
        << sizeof(anArray) / sizeof(anArray[0]) << endl;
        }

        void funcCalledTheRightWay(int* anArray, int arraySize){
        cout << "I'm in funcCalledTheRightWay" << endl;
        cout << arraySize * sizeof(anArray[0]) << endl
        << sizeof(anArray[0]) << endl
        << arraySize << endl;
        }

        int main()
        {
        int size = 10;
        int array[size];

        cout << "I'm in main" << endl;
        cout << sizeof(array) << endl
        << sizeof(array[0]) << endl
        << sizeof(array) / sizeof(array[0]) << endl;

        funcCalledIncorrectly(array);
        funcCalledTheRightWay(array, size);

        cin.get();
        return 0;
        }

         
    • aditsu

      aditsu - 2004-09-23

      if it's a zero-terminated string then you can safely use the strlen function
      otherwise you can't know the size just from the pointer

      Adrian

       

Log in to post a comment.