r/learncpp•
Stvdent

If C++ doesn't "remember" the size of arrays (we use the address of the start of the array), then why is there a difference between "delete" and "delete []"?

For example, wouldn't using "delete" on the address of a dynamic array of size 1 be interpreted the same as using "delete" on an address stored in a pointer? Essentially, as I see it, the same operation is happening: memory is freed at that address, and C++ doesn't "know" whether or not that is the start of an array or not, just that it is a place in memory.

Posted Apr 24, 2021, 1:27 AM UTC

2513
jedwardsol•

The implementation of new[]/delete[] knows how big the buffer is and how many destructors to call.

But that information is not exposed to the program.

9
StvdentOP•

knows how big the buffer is

I'm not sure what a buffer is. Do you mean that it knows the size of the array?

2
jedwardsol•

Yes

2
StvdentOP•

Then why is that information not accessible? Thank you for taking the time to provide these answers, by the way.

1
jedwardsol•

Because the creators of C and C++ decided you didn't need to know.

There are implementation specific ways

https://docs.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapsize

https://man7.org/linux/man-pages/man3/malloc_usable_size.3.html

The standard C++ way is to use a std:: vector, which remembers the size for you.

3
[deleted]•

[deleted]

7
StvdentOP•

Thank you for the response.

the array still allocated lots of memory for all the array elements

Honestly, his sounds wasteful. If this is true, wouldn't that mean that "int *ptr = new int[1]" would allocate "lots" more memory than "int *ptr = new int" would? If that's the case, then that's a drawback to using, say, an array of 10 int elements vs. initializing 10 separate int variables.

Why even allocate more memory than what is needed?

3
HappyFruitTree•

wouldn't using "delete" on the address of a dynamic array of size 1 be interpreted the same as using "delete" on an address stored in a pointer?

Not necessarily. For types that are not trivially destructible delete[] needs to look at the size and then call that number of destructors while delete always calls exactly one destructor.

3
StvdentOP•

My confusion came up because I thought that C++ didn't store the information about an array's size because that information is not accessible. Apparently, users cannot access that information (for arrays) but the information is still kept track of.

1
HappyFruitTree•

In theory you could imagine that it doesn't keep track of the length of the array for trivially destructible types such as int that doesn't need a destructor to be called but I have no clue whether implementations actually do it like that.

If there was a function or operator that allowed you do get the size of a dynamically allocated array you would have to be very careful that it actually is a dynamically allocated array and not a subobject of another object or a local variable. It would be very easy to misuse.

You might be tempted to write something like this:

void print(int arr[])
{
	int length = get_dynamic_array_size(arr);
	for (int i = 0; i < length; ++i)
	{
		std::cout << arr[i] << "\n";
	}
}

And assuming you can use get_dynamic_array_size (which is my made-up function for getting the size of a dynamically allocated array) on an array of int despite that it is trivially destructable type the following would work fine:

int* my_array = new int[3]{1, 2, 3};
print(my_array); // OK

But the following would not:

int arr[3] = {1, 2, 3};
print(arr); // UB

struct A { int arr[3] = {1, 2, 3}; };
A* a = new A;
print(a->arr); // UB

int (*m)[3] = new int[1][3]{{1, 2, 3}};
print(m[0]); // UB

Since the function takes a pointer you might also be tempted to pass a pointer to the middle of the array in order to only print part of the array (this is very easy to do with functions that takes a pointer and a size) but it's not clear what get_dynamic_array_size would do in this case. It would probably lead to undefined behaviour right away but if it returned the length of the whole array that would also not work in this case.

int* my_array = new int[3]{1, 2, 3};
print(my_array + 1); // UB
1
mushynelliesandy•

Is there any benefit to still using arrays given vectors know their size and are dynamic in the fact you can add/remove elements as necessary? Excuse any potential inaccuracies I’m still leaning.

1
iBoo9x•

I think it should be like this: When we allocate memory, the amount of memory is known for sure. When we free memory using delete, base on the data type that the pointer point to, it calls the destructor, and free that amount of memory. About delete[], it just do the same but also call destructor of each element. How the program know the size? When we know the amount of memory and the data type (the size of that data type), we can easily caculate the size of the array, right?

1