2

I was trying to see if a dynamically created array has the same behavior as an ordinary array, but it looks like they are indeed a bit different. This is the code that I used to test their behaviors:

int main(){
    int *int_array{new int[10]};
    cout<<sizeof(*int_array);
    return 0;
}

The output displayed to the console is: 4

But for an ordinary integer array, upon using the sizeof function, I get the total size occupied by the array in bytes. As shown by the code below:

int main(){
    int int_array[5];
    cout<<sizeof(int_array);
    return 0;
}

The output displayed to the console is: 20

So it seems that when I am dereferencing a pointer to a dynamically allocated array, at least for the "sizeof" function, I can only "see" the first element of the array, not the entire array at a glance.

Is this correct? Can you explain what's happening here??

I expected to see uniform, consistent behavior for all types of arrays, regardless of how they were created and stored in memory. I have tried googling to see the differences between Dyn. allocated arrays and ordinary ones, but they didn't yield anything explaining this.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    In `int *int_array{new int[10]};` int_array is not an array, it is a pointer to an int. It is being used to point to the first element of an unnamed dynamically allocated array & when combined with knowledge of the number of elements that are in that array, it allows access to and use of that unnamed array. – Avi Berger Dec 13 '22 at 02:35
  • See [What does new operator returns](https://stackoverflow.com/questions/32453095/what-does-the-new-operator-returns) and [what is the return type of new](https://stackoverflow.com/questions/2697892/what-is-return-type-of-new-in-c) – Jason Dec 13 '22 at 03:06
  • Also [How to get size of dynamic array](https://stackoverflow.com/questions/22008755/how-to-get-size-of-dynamic-array-in-c) – Jason Dec 13 '22 at 03:08
  • @JasonLiam All the questions you linked look at each facet of the problem in isolation. This is hardly a duplicate of them – fjch1997 Dec 13 '22 at 03:11
  • @fjch1997 Not everything should be spoon feeded. OP should be able to figure out by reading the individual dupe targets what is going on. – Jason Dec 13 '22 at 03:13

2 Answers2

1

Going off pure instinct, rather than testing this, the sizeof the static array (20) is convieniently a multiple of 5 - the records you declared when initialising the array, and 4 - the size of the *int_array from your first main() function.

The static array sizeof is giving the total size of the statically declared array. The sizeof the contents of the dynamic int_array is the size of what it points to, the head of the array.

This has peeked my interest, I'm going to have a little play. But my feeling is that, rather than being a difference in behaviour between static and dynamic arrays, its more a difference of how sizeof interprets what it has been given. so int intArray[5]; is not a pointer, its statically declared and using the data memory of the program. Its dimensions are known. Its an integer array, so its size is 4 bytes (for an int) * 5 (number of elements).

Whereas int *intArray{new int[10]}; is instanced with a new, so its living on the heap. But it's a pointer, and we know that pointers just point to an area of memory. So sizeof just gives the block that the pointer is pointing to, which we know is the first element of a dynamically created array, its an integer so its 4 bytes.

Edit: continuing my musing... getting the size of the whole dynamic array, to mimic what happens with sizeof a static array, we can add all the elements together and expect to see the same answer doubled (since you declared the dynamic array as 10, not 5 as is the static array)

#include <iostream>

#define ARRAYSIZE 5
using namespace std;

int main()
{
    int *int_array{ new int[5] };
    cout << "Saying:\n";
    cout<<sizeof(*int_array);
    cout << "\nIs the same as saying:\n";
    cout<<sizeof(int_array[0]);
    cout<<"\n";
    cout << "Mimic sizeof(static_array), we need to:\n";
    int size = 0;
    int i = 0;
    for (i = 0; i < ARRAYSIZE - 1; i++) {
        size+=sizeof(int_array[i]);
        cout<<sizeof(int_array[i]);
        cout<<" + ";
    }
    cout << sizeof(int_array[i+1]);
    cout << " = ";
    cout<<(size+=sizeof(int_array[i+1]));
    cout << "\nwhich is the same size as the static sizeof!\n";

    return 0;
}

And running, gives:

    noscere@bertram:~/src/Stack/dynamicArrays$ g++ dynArray.cpp -o dynArray
    noscere@bertram:~/src/Stack/dynamicArrays$ ./dynArray 
    Saying:
    4
    Is the same as saying:
    4
    Mimic sizeof(static_array), we need to:
    4 + 4 + 4 + 4 + 4 = 20
    which gives us the same size as the static sizeof!

Change ARRAYSIZE to 10, as you originally declared, we get 40.

Noscere
  • 417
  • 3
  • 8
  • Trying to be concise (not my strongpoint!) if you did sizeof int_array[0] + int_array[1] .. etc, you get the size of the whole array (which, if you change 10 to 5 to match the static array) is 20. – Noscere Dec 13 '22 at 02:51
1

With some help from the IDE, you can see the difference in the type.

First example

enter image description here

This is of type int[5], with its size included. Therefore, sizeof(int[5]) == 20

It is implicitly convertible to int*

enter image description here

sizeof(int*) == 8

Assigning it to a reference will retain the type of int&[5].

sizeof(int&[5]) == 20

enter image description here

Second example

Heap allocated array always have int* as the return type.

sizeof(int*) == 8

enter image description here

Further reading on different array types

https://en.cppreference.com/w/cpp/language/array

fjch1997
  • 1,518
  • 18
  • 19
  • Nice use of the IDE @fjch1997 - I hadn't thought to throw it into vscode (or whatever editor of choice may be) - I was simply using good ol' nano :smiley face: :big thunmbs up for fjch1997: *edit why hasn't stack implemented emojis :big sad face:* – Noscere Dec 13 '22 at 03:13