How to find size of a dynamically allocated array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tapas Bose
    New Member
    • Mar 2010
    • 1

    How to find size of a dynamically allocated array

    Hello.
    I want to find size of a dynamically allocated array in my following code :
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
            
            int *Set = NULL;
            int element;
            char Choice;
            int n = 0;
            
            do
            {       
                    cout << "  Enter an element : ";
                    cin >> element;
                    
                    n++;
                    Set = (int *)realloc(Set, n * sizeof(int));
                    Set[n - 1] = element;
                    
                    cout << "  Do you want to enter another element? (y/n) : ";
                    cin >> Choice;
            } while (Choice == 'y' || Choice == 'Y');
            
            cout << "  Size of the array : " << sizeof(Set)/sizeof(int) << endl;
            return 0;
    }
    Here is the output :
    tapas@My-Child:~/Programming/Set Intersection$ ./test.o
    Enter an element : 1
    Do you want to enter another element? (y/n) : y
    Enter an element : 2
    Do you want to enter another element? (y/n) : y
    Enter an element : 3
    Do you want to enter another element? (y/n) : n
    Size of the array : 1 <-- But the size should be 3!!
    The reason behind this behavior is, Set is a integer pointer which points to a chunk of contiguous memory block and using sizeof(Set)/sizeof(int) I am getting the size of the pointer (am I wrong?). But how can I can the size of allocated memory, I also try it sizeof(*Set)/sizeof(int), but the result is same. And
    Code:
     cout << "  Size of the array : " << n  << endl;
    will give the answer. But I need some function. Please help.
  • colford
    New Member
    • Mar 2010
    • 1

    #2
    You have declared:

    int *Set = NULL;

    this means that the variable Set is a pointer to int. On your system pointers are the size of 4 bytes which is why you get the answer 1 when you do sizeof(Set)/sizeof(int). Instead of using dynamic allocated memory why not let the standard template library do all the heavy lifting for you by using a vector.

    i.e.

    vector<int> Set; // to create it

    Set.push_back( element ); // to add a new int to it

    Set.size(); // to get the size

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      The actual answer to your question is that there is no way to get the size of a dynamically allocated array except to use a variable, like n, to remember what it was. There is no function that returns it.

      Since you are using C++ you should avoid using malloc/realloc/free although C++ admitidy has no equivilent of realloc.

      And as colford says you should prefer using vectors and other standard contains in C++ rath than arrays and other home grown containers.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        I think you are getting the correct answer.
        sizeof(Set)=3
        sizeof (int) probably = 4 on your machine
        int 3/4=1

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          I want to find size of a dynamically allocated array
          In a sense your question is redundant when you consider the way in which a dynamic array is typically declared in C++:

          double* p = new double [20]; // so the size of p is 160 bytes

          As far as I am aware setting the number of elements in the subscript of array p (or equivalent) is mandatory.
          Of course you have to call <delete [] p> later to prevent the possibility of dangling pointers and memory leaks.

          Comment

          • lambrospower
            New Member
            • Mar 2010
            • 1

            #6
            i think if you don't set the SET = NULL at the beginning it will work. it does in c though!

            but you must use an
            if n= 0 malloc
            in the do while

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by whodgson
              I think you are getting the correct answer.
              sizeof(Set)=3
              sizeof (int) probably = 4 on your machine
              int 3/4=1
              Erm, sizeof(Set) is not 3, remember sizeof returns the size in bytes of an object, not the number of elements in an array since sizeof can be called on things that are not arrays.

              As has already been said Set is a pointer sizeof does not do any dereferencing, it returns the size of the object passed to it not the size of the object pointed to by the object passed to it so sizeof(Set) is sizeof a pointer which for a 32 bit machine is 4 bytes.

              Additionally even if it did return 3 then

              int 3/4 = 0 not 1

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Yes #4 is very bad ...i wonder where I was when that was written. Please ignore thread #4
                ......thanks Banfa

                Comment

                Working...