I want to write a platform independent program that would use up at
least 5Gb of memory for testing purposes. How would I do that?
Not possible. Some platforms have an address space smaller than
5 Gb. It isn't uncommon on platforms with a 32 bit address space
for a maximum of 2 Gb to be usable by a program (with the
remaining 2 Gb of the address space reserved for the OS.) The
same applies to platforms that are internally more than 32 bits
but give each program a 32 bit virtual address space; on some
such systems, privileged programs could access more memory, but
the mechanisms for that would be completely system dependant.
You can probe the number of bits available in size_t and determine
if it has more than 32 bits; if it does, then you could attempt
to malloc() the 5 Gb. Note though that some systems will permit
you to malloc() the memory while not actually having that much
memory available, so to actually get ahold of the memory, you
may have to scribble something on every memory "page" (an OS
concept, not a C concept); unfortunately you may have to
make assumptions about the minimum size of a memory "page" in order
to do the scribbling in a reasonable time.