Global Variables and Page Faults

Excuse me if the question has already been answered on stream, I've only been able to watch about 50 of them.

I've been programming in c for a little bit over a year, but never really used global variables a lot. However today I was trying to improve my SIMD game by writing a fast(ish) memset, and happened to have huge global buffers. Which resulted in a page fault.

Now to the question:

1
2
3
4
5
6
int buffer[10000];
int main()
{
    //stupid slow memset body for illustration
    for(int i = 0; i<10000;i++)buffer[i]=0;
}

Apparently causes a page fault. Is the buffer only reserved, not commited?.

Also the default memset seams to have no problem with this. Does it have __try, __catch to handle the page fault or is it solved in a better way?

As a side note: I can't seam to make a implementation with _mm_stream_si128 faster than _mm_storeu_si128 even for large memsets any pointers?

Edited by Daniel Hesslow on
Well, I think there's something suspicious going on here - I think it would be extremely unusual for any short-addressable (eg., less-than-64k) global to have a problem like that? I think maybe the C spec even guarantees that you can have objects of that size? Anyway, I don't remember, but just to see, I tried compiling exactly that code you wrote, and I can run it just fine - no page fault. This is on VC++ on Windows.

In general, uninitialized globals are marked in the BSS segment, as far as I know, and while the OS may place practical limits on the total size of the BSS (I'm not sure what they are on x64 Windows, I'm not sure where that is specified), I'm very confident that it is never less than 64k :)

- Casey
Hmm. Sadly the code is gone now since I've been twiddling around with it and I can't reproduce it with the new code. I seem to have boiled it down too much when I posted it here since I mostly wanted to show the gist of it. I totally should have made sure that the gist of it actually had the same bug.. Sorry. Hope I didn't waste to much of your time!

Good thing there wasn't some new weird catch in c that I didn't know about :p

Probably was just being a bit retarded and missed something, anyway, thanks!

I have used global byte buffers with size around 128MB - works fine on all platforms I needed at that time (Windows, OSX, Linux).