I have to make a program that would allocate an array of
void segfault_sigaction(int signal, siginfo_t *si, void *arg)
printf("ujel segafult, velikost pomnilnika je: %d bajtov\n", sizeof(int) * i);
int main(int argc, char* argv)
int n = atoi(argv);
struct sigaction sa;
sa.sa_handler = segfault_sigaction;
//sa.sa_sigaction = segfault_sigaction;
//sa.sa_flags = 0;
addr = mmap(NULL, n * sizeof(int), PROT_WRITE | PROT_READ ,MAP_ANONYMOUS | MAP_PRIVATE,-1,0);
i = n;
addr[i+1] = 5;
The amount of memory mapped by
mmap() is just what you requested, perhaps rounded up a bit. If you're asking, "How can I see how much it rounded up," the answer is you can't do so in a general way, but some specific platforms may have APIs that let you figure it out. But there's no reason why you'd need to.
The code you've written seems to be "searching" for the end of the allocation by looking for a nonzero part. This results in undefined behavior because you will always run off the end of the allocation. You absolutely must not do this. Just use the size that you passed to
mmap() as the size. That's the only size that matters.