[Gllug] Memory Usage

Nix nix at esperi.org.uk
Tue Mar 11 22:19:51 UTC 2008


On 10 Mar 2008, Stephen Nelson-Smith stated:
> Incidentally (and thinking of the C thread on this list), one of the
> developers wrote this to test:
>
> int main(int argc, char **argv) {
>     unsigned long lump = 2L * 1024L * 1024L * 1024L + 1L;
>     unsigned char *thereabouts = (unsigned char *)malloc(lump);
>     thereabouts[lump - 1L] = 'a';
>     printf("0x%x (%u) bytes starting at %p\n",
>                        lump, lump, thereabouts);
>     return 0;
> }
>
> I can't see what this does that user level tools don't do, but my C is
> dreadful.  Anyone care to elucidate?

I suspect the developer thought it would allocate that much memory:
but with overcommitment on, it won't: all it's doing is showing that
you have at least that much contiguous address space.

He should have calloc()ed it instead: the zero-filling done by calloc()
touches each page and thus allocates the memory (and smashes the dcache,
but you can't avoid doing that when actually allocating memory unless
you want a nice big security hole).

-- 
`The rest is a tale of post and counter-post.' --- Ian Rawlings
                                                   describes USENET
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list