I know I shouldn’t use Dev-C++ but it’s mandatory in school so I can’t do anything about it.
Topic is pointers in C/C++ and a bug occurred while measuring the length of an integer array. See the code below:
// POINTER
# include<iostream>
# include<string.h>
using namespace std;
int main(){
//neues Feld anlegen
int *a = new int[5];
a[0] = 12;
a[1] = 5;
a[2] = 43;
a[3] = -12;
a[4] = 100;
// Feld füllen
for(int i = 0; i<sizeof(a);i++){
cout<<a[i]<<"\n"<<endl;
}
cout<<sizeof(a);
system("pause");
return 0;
}
The sizeof() returns 4 and not 5…Any ideas?
It is not a bug because its returning the size of
aitself (which is of typeint*– 4 bytes on a 32 bit build), not the length of the array.Please note – others say that the size is dependent on the operating system, which is half-true. It really depends on the build.
sizeofis a compile-time construct.