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;
}