size of dynamic array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenneth6
    New Member
    • Mar 2007
    • 79

    size of dynamic array

    Code:
    int* x=new int[10];
    cout << (sizeof(x)/sizeof(int)) << endl;
    x[0]=10;
    x[1]=20;
    x[2]=30;
    x[3]=40;
    cout << x[0] << endl;
    cout << x[1] << endl;
    cout << x[2] << endl;
    cout << x[3] << endl;
    cout << x[4] << endl;
    cout << (sizeof(x)/sizeof(int)) << endl;
    Why can't i get the number of elements in this array?
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    sizeof, when used on a pointer, returns the size of the pointer. The sizeof of a pointer is the number of bytes in one 'word' of the computer, 4 for a 32bit machine. An int is also 4 bytes on a 32bit machine, so that will print out 1 everytime on nearly every computer.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      The convenient, simple, and most often used method for figuring out the size of an array is...not to figure it out. Make it a separate variable, or if it's a function, require an additional argument indicating the size. Especially for the function, if someone gives you the wrong size, well, that's their own fault.

      Comment

      • kenneth6
        New Member
        • Mar 2007
        • 79

        #4
        Code:
        cout << x[4] << endl;      // within the max boundary of array, but not initialized
        cout << x[10] << endl;     // beyond the max boundary of array
        cout << x[11] << endl;
        cout << x[12] << endl;
        if i print the above three lines in the above situations, what numbers will be printed out? are the numbers meaningful?

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          No one knows. When you create an array, all it is is a block of memory. So when you say:

          [code=cpp]int myArray = new int[10];[/code]

          somewhere in memory, there is a space 10 * sizeof(int) long (probably 40 bytes):

          Code:
           [memory][myArray[0]][myArray[1]]...[myArray[9]][more memory]...
          So if you use myArray[10] or anything else past the bounds, you are looking into memory which may or may not be allocated for your program. You have no idea what you might be printing out. You'll be lucky not to get a segmentation fault for accessing illegal memory locations.

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            You'll get garbage, whatever happened to reside in memory at those locations. If you're lucky, that is. If you're not, it's possible to get a segfault from that.

            Comment

            • kenneth6
              New Member
              • Mar 2007
              • 79

              #7
              Originally posted by Ganon11
              If you use myArray[10] or anything else past the bounds, you are looking into memory which may or may not be allocated for your program. You have no idea what you might be printing out. You'll be lucky not to get a segmentation fault for accessing illegal memory locations.
              Thanks! I don't know about the hardware memory. May I know if it is possible to print out either 0, 1, 2 or 3 in the output?

              Comment

              • kenneth6
                New Member
                • Mar 2007
                • 79

                #8
                Originally posted by Ganon11
                The convenient, simple, and most often used method for figuring out the size of an array is...not to figure it out. Make it a separate variable, or if it's a function, require an additional argument indicating the size. Especially for the function, if someone gives you the wrong size, well, that's their own fault.
                Yes, right ! Ganon11. I do agree with you. Avoid figuring it out!!

                I tried to solve by adding a tracking variable!!

                this is the most simple solution

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  OK. Here's how it works:

                  1) sizeof returns the s1ze of the object on the stack.

                  That makes:

                  [code=c]
                  char arr[5];
                  cout << sizeof(arr) << endl;
                  [/code]

                  display a value of 5. That is, arr occupies 5 bytes.

                  This:
                  [code=c]
                  char* arr = new char[5];
                  cout << sizeof(arr) << endl;
                  [/code]

                  displays 4. All that's on the stack is the pointer to the array.

                  As a rule, do not use sizeof to find the number of elements in an array.
                  1) It does not work for heap arrays
                  2) It does not work inside functions where the array is passed in.

                  Instead, use a const int var for the number of elements and pass that variable around alogn with the array address.

                  Comment

                  Working...