I'm writing a program which accepts the size of an array as an argument which should then dynamically allocate memory for that size.
I want my program to return the size of the memory which was allocated.
Currently I'm using argv
to read the input value and then mmap
for the dynamic allocation. I also use a for loop to display what number is saved in each index of the array.
I know allocating could be done with malloc
, but I'm not able to use it.
I guess I need to start at the last element and check all cells until I get a SEGFAULT signal.
Thanks for your help.
EDIT: Short code without libraries etc...
input = (atoi(argv[1]));
int* arr = mmap(0, input, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
for (i = 0; i < input; i++) {
arr[i] = i + 1;
}
You can't check the amount of allocated space in C or check the size of arrays. You need to save it as a variable explicitly by yourself.