How to create a process that uses 5Gb of memory?

R

Richard Heathfield

santosh said:
In C a char is always a byte.

Anyone who can go a week (let alone many, many months) in this newsgroup
without learning that a char is defined to be 1 byte wide is either
unable or unwilling to learn.
 
R

Richard

Richard Heathfield said:
santosh said:


Anyone who can go a week (let alone many, many months) in this newsgroup
without learning that a char is defined to be 1 byte wide is either
unable or unwilling to learn.

Is that you being a snide, self absorbed bighead again, or did you have something to
offer the thread?
 
M

Malcolm McLean

P said:
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?

Thanks!

#include <stdio.h>
#include <stdlib.h>

char *encyclopedia;

int main(void)
{
encyclopedia = malloc(5 * 1024 * 1024 * 1024 );
if(!encyclopedia)
{
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
gets(encyclopedia);
printf("Your encylopedia is %s\n", encyclopedia);
return 0;
}

However it is unlikely that you will be lucky enough to run on a platform
with 64 bit ints. So put casts to size_t and l suffixes etc into that
malloc() call until it seems to work.
There are two ways in which this program could fail, unrelated to the main
problem. One is obvious, but the reasoning gets a little hollow at times.
The other is more serious.
 
S

santosh

Malcolm said:
#include <stdio.h>
#include <stdlib.h>

char *encyclopedia;

int main(void)
{
encyclopedia = malloc(5 * 1024 * 1024 * 1024 );

I suppose you didn't read the previous articles in this thread?
if(!encyclopedia)
{
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
gets(encyclopedia);

No need to recommend dangerous functions to possible newbies.
printf("Your encylopedia is %s\n", encyclopedia);
return 0;
}

However it is unlikely that you will be lucky enough to run on a platform
with 64 bit ints. So put casts to size_t and l suffixes etc into that
malloc() call until it seems to work.

size_t is not guaranteed to be able to hold that value either.

[snip]
 
S

Stephen Sprunk

jacob navia said:
True. I should have added some test along the lines of
if (CHAR_BIT*sizeof(size_t) > 33 && s = malloc(...);

That's not guaranteed to work either, even if there's 5GB of memory
available. size_t is only guaranteed to have at least 16 value bits; a
perverse implementation may have a 64-bit address space but a smaller
size_t, meaning you have to malloc() what you need in chunks no larger than
SIZE_MAX. The OP didn't specify he had to have his 5GB in a single
object...

However, I can't see any way a system could have 5GB of memory accessible if
(CHAR_BIT*sizeof(void *)) < 33. Of course, on most systems, sizeof(size_t)
== sizeof(void *), but the interesting cases are where that's not true.

S
 
I

Ian Collins

Stephen said:
That's not guaranteed to work either, even if there's 5GB of memory
available. size_t is only guaranteed to have at least 16 value bits; a
perverse implementation may have a 64-bit address space but a smaller
size_t

How could it? size_t is defined as the result of the sizeof operator,
so the implementation would be limited to 64K objects. Perverse, but
possible I guess.
 
¬

¬a\\/b

Like this:
#include <stdlib.h>
int main(void)
{
char *s = malloc(5*1024*1024*1024);
}

#include <stdlib.h>
int main(void)
{char *s, *s1, *s2;

s = malloc(2*1024*1024*1024);
s1 = malloc(2*1024*1024*1024);
s2 = malloc(1024*1024*1024);

return 0;
}
 
¬

¬a\\/b

#include <stdlib.h>
int main(void)
{char *s, *s1, *s2;

s = malloc(2*1024*1024*1024);
s1 = malloc(2*1024*1024*1024);
s2 = malloc(1024*1024*1024);

return 0;
}


#include <stdlib.h>
int main(void)
{int i;
char *s[1024];

if(5*1024*1024<=1024 || 1024*1024<=0)
return 0;
for(i=0; i<1024; ++i)
{s=malloc(5*1024*1024);
if(s==0) break;
}
Sleep(1200);
if(i>0)
for(--i; i>=0; --i)
free(s);
return 0;
}

don't know if it is ok
 
¬

¬a\\/b

this seems compilable in windows

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

int main(void)
{int i;
char *s[1024];

if(5*1024*1024<=1024 || 1024*1024<=0)
return 0;
for(i=0; i<1024; ++i)
{s=malloc(5*1024*1024);
if(s==0) break;
}
printf("Presa memoria=%gbytes i=%i\n",
i*1024.0*1024.0*5, i);
Sleep(12000);
printf("Esco\n");
if(i>0)
for(--i; i>=0; --i)
free(s);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top