(First of all : This code is intented to run only on Windows10 but behavior on other OS is interesting. Of course, change _msize by the appropriate function)
Part I of the code. Memory allocation is done with malloc and I use _msize to print the element number of an array in a function (I know that, for memory allocation reason ..., I could have a superior value but I have not yet observed such a case. If you have real case where it happens, I am interested but it is not the main reason of my question )
Part II of the code : Same idea but with variable lenght array. I have two questions. First, is there an equivalent of _msize for VLA ? and second, my program hangs, why does not _msize return ?
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
void myfoo(int *array) {
printf("Nb elt of an array obtained from its pointer %d\n",_msize(array)/sizeof(int));
}
int main() {
int n=1;
printf("Part 1 : malloc\n");
while(n>0) {
printf("n : ");scanf("%d",&n);
if (n<0) break;
int *array;
array=malloc(n*sizeof(int));
for (int i=0;i<n;i++) {array[i]=i;}
myfoo(array);
free(array);
}
n=1;
printf("Part 1I : Variable Length Array\n");
while(n>0) {
printf("n : ");scanf("%d",&n);
if (n<0) break;
int array[n];
for (int i=0;i<n;i++) {array[i]=i;}
myfoo(array);
}
}