is it possible to find out the page size of a processor using C program and malloc? and not by using sysconf() call?
Standard C has no concept of a "page size" so, based on your tags, no. There may be an implementation-specific method - do you have a particular aversion to using sysconf
? I'm assuming you mention it because it's a way to actually get the information you need, in which case, why not use it?
Malloc isn't likely to tell you what you want to know. While it often has minimum sizes and alignment considerations of what it will allocate, they aren't necessarily the same as (or related to in any defined way) the page size. Are you willing to look elsewhere? What environment are you running?
If you can #include some linux kernel header, you can find the macro PAGE_SIZE in
<asm/page.h>
On cygwin (and perhaps windows' ddk) i's in
<w32api/ddk/winddk.h>
There are "dirty tricks" (very, very dirty!) to calculat the page size at runtime. All unportable and totally system-dependent.
Let's have some fun!
A possible trick on may systems is to create a shared memory object only 1 byte long. The system call usually rounds the size up to the system page size. Bleach!
Another one, if you are running on unix-like systems and you dare to intercept the SIGSEGV signal, you could try to explore the memory by looking for valid areas and see which power of 2 they are rounded. Uhm...
So, why sysctl is not good for you?
Regards
(its not really the answer, but I am unable to add it in comments section, so replying here: its just some results from my investigation)
to find out page size of system/microprocessor without using sysconf is just for fun! This is what I tried so far:
-get the actual page size using sysconf(_SC_PAGE_SIZE): I got in this computer 0x1000 which is 4K (its intel 386 processor with 32-bit linux loaded in it.
-then I allocated 1 byte memory
-in a loop, using offset counter, I wrote beyond 1 byte allocated memory. it worked fine till 0x19a8 which is beyond 1 page size and just below 2 page size!
-SEGV happens somewhere between 1page to 2page size, towards the end. This is repeatble.
-so this method couldn't find the page size reliably.
That's why I asked this question. Is there a trick (other than using direct information available in kernel) to find out the page size? I am even ready to compromise on malloc (means using some other trick).