I accidentally asked myself a question about arrays in C ++. Well, we all know that arrays are fixed collections of something, I say it’s fixed, because when defining arrays it is necessary to declare the length of the array. So, consider an example:
char myarray[10] = {'\0'};
int sz = sizeof(myarray);
Well that's right, 10 is the number returned by sizeof. This can be done by the compiler because it knows how much space it has allocated for this variable.
Now consider what happens in this situation:
void dosome(mystruct* arr) {
int elements = sizeof(arr)/sizeof(mystruct);
for (int i = 0; i < elements; i++) {
}
}
Nice ... but I suppose this could be crowded. If I pass this function an array that I created in the “normal” way, everything should be fine:
mystruct array[20];
dosome(array);
No problems. But if I do this:
mystruct* array = (mystruct*)malloc(80*sizeof(mystruct));
dosome(array);
???????????????????
, sizeof, ??? , , , - , , ? , malloc dosome . ?
, sizeof.
.