0

I was wondering how sizeof operator and delete[] got to know the actual size of the array they are operating on, but when I pass an array as an argument to functions, that size information is lost and the size information needs to be passed into the function explicitly by the programmer?

What is the internal mechanism that resulted in this discrepancy?

#include <iostream>

void my_function(int* p, int n) {
    std::cout << "when passed into a function as argument, array size information is not available" << std::endl;
}

int main() {

    int a[] = { 1, 2, 3 };
    std::cout << "sizeof(a): " << sizeof(a) << std::endl; // size information available to `sizeof` operator

    int* b = new int[3] {1, 2, 3};
    delete[] b; //size information available to `delete[]` operator

    int c[] = { 1, 2, 3 };
    my_function(c, 3);

    return 0;
}
Thor
  • 9,638
  • 15
  • 62
  • 137
  • What makes you think that the "size information is available" in the case of the `b` pointer in your code? It may (must) be available *internally* to the system's memory allocator, but try showing some code where you can fetch the size of the `b` array *solely* from the pointer itself (like if you pass *that* pointer to another function). – Adrian Mole Jun 19 '20 at 02:00
  • I may have not made my point entirely clear, when using `delete[]`, the array size information is not available to the programmer, but it is available to `delete[]`, that's how `delete[]` can deallocate memory assigned to the array, right? – Thor Jun 19 '20 at 02:01
  • Yes! Just as it would be in the case of a pointer returned by `malloc` (which may even be used to implement the `new[]` operator). To your C++ code, it's just an address; to the system, it is an address that is somewhere/somehow indexed into a system-wide memory manager's list of allocated blocks. – Adrian Mole Jun 19 '20 at 02:03
  • so the size information is available to the `delete[]` operator, but why can't that same information be made available for arrays passed in as an argument to a function? – Thor Jun 19 '20 at 02:08
  • 1
    It's *not* available to the `delete[]` operator. It is available to the system-specific *implementation* of that operator. A pointer in C++ is ***just*** an address. Passing such a pointer to a function does not convey any information to that function about the size of the data block, whether that pointer is from `new[]`, `malloc` or the address of a fixed, user-declared array. It is *still* just a pointer. – Adrian Mole Jun 19 '20 at 02:12

1 Answers1

2

The reason the delete[] operator knows how much memory is associated with a particular pointer value is because it has a copy of that value saved in its internal data structure that was written when new[] was used. From strictly a language standpoint, it doesn't know how much memory a pointer points to.

There's also a requirement that a pointer pass to delete[] must have been returned by new[]. Passing in any other pointer value results in undefined behavior.

The sizeof operator only looks at the type of its operand and is thus evaluated at compile time. In your code, the sizeof operator can see that its operand has type int[3] and evaluates to the size in bytes of the array (12 on most systems).

When you pass an array to a function, it decays to a pointer to the first element of the array. So in my_function you only have a pointer. You could have just as easily done something like this:

int x;
my_function(&x, 1);

So if you were to use sizeof on the parameter p it looks at the type of the operand which is int * and evaluate to the size of the pointer (most likely either 4 or 8).

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Just wondering, why can't they make the "copy of that value saved in its internal data structure" available to programmer? – Thor Jun 19 '20 at 02:44
  • @Thor Depending on the library, it may be available. On Linux, there's a function called `malloc_usable_size` that returns the number of bytes a malloc'ed pointer points to. Stuff like is very system specific, however. – dbush Jun 19 '20 at 02:54
  • 1
    @Thor Loose pronouns lose understanding. ;) When you wrote that comment, did you have a clear picture of who "they" are? In this context, there is a huge difference between "they" meaning the people writing C++ specs and "they" meaning the people implementing C++ on a particular system. – JaMiT Jun 19 '20 at 02:58