Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to create a process that uses 5Gb of memory?

Reply
Thread Tools

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

 
 
Richard Heathfield
Guest
Posts: n/a
 
      08-06-2007
santosh said:

> Richard wrote:
>>
>> Is a char a byte now? I lose track.

>
> 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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      08-06-2007
Richard Heathfield <(E-Mail Removed)> writes:

> santosh said:
>
>> Richard wrote:
>>>
>>> Is a char a byte now? I lose track.

>>
>> 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.


Is that you being a snide, self absorbed bighead again, or did you have something to
offer the thread?
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      08-06-2007

"P" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-06-2007
Malcolm McLean wrote:

>
> "P" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>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 );


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]

 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      08-07-2007
"jacob navia" <(E-Mail Removed)> wrote in message
news:46b5b904$0$5093$(E-Mail Removed)...
>
> 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

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-07-2007
Stephen Sprunk wrote:
> "jacob navia" <(E-Mail Removed)> wrote in message
> news:46b5b904$0$5093$(E-Mail Removed)...
>>
>> 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


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.

--
Ian Collins.
 
Reply With Quote
 
Ča\\/b
Guest
Posts: n/a
 
      08-07-2007
On Sun, 05 Aug 2007 13:03:49 +0200, jacob navia
<(E-Mail Removed)> wrote:

>Army1987 wrote:
>> On Fri, 03 Aug 2007 13:56:44 -0700, P wrote:
>>
>>> 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?

>>
>> Am I actually reading "platform independent" and "5Gb" in the same
>> sentence?
>>
>> (fx:removes glasses and looks closer)
>> Yes, I am.
>> The laptop I'm using right now has 1GB RAM and 1GB swap partition.
>> How can you use 5GB of memory on it?

>
>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;
}

>This is platform independent.
>
>Whether it works is platform dependent.


 
Reply With Quote
 
Ča\\/b
Guest
Posts: n/a
 
      08-07-2007
On Tue, 07 Aug 2007 07:39:49 +0200, "Ča\\/b" <(E-Mail Removed)> wrote:

>On Sun, 05 Aug 2007 13:03:49 +0200, jacob navia
><(E-Mail Removed)> wrote:
>
>>Army1987 wrote:
>>> On Fri, 03 Aug 2007 13:56:44 -0700, P wrote:
>>>
>>>> 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?
>>>
>>> Am I actually reading "platform independent" and "5Gb" in the same
>>> sentence?
>>>
>>> (fx:removes glasses and looks closer)
>>> Yes, I am.
>>> The laptop I'm using right now has 1GB RAM and 1GB swap partition.
>>> How can you use 5GB of memory on it?

>>
>>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;
>}



#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[i]=malloc(5*1024*1024);
if(s[i]==0) break;
}
Sleep(1200);
if(i>0)
for(--i; i>=0; --i)
free(s[i]);
return 0;
}

don't know if it is ok

>>This is platform independent.
>>
>>Whether it works is platform dependent.

 
Reply With Quote
 
Ča\\/b
Guest
Posts: n/a
 
      08-07-2007
On Tue, 07 Aug 2007 08:57:41 +0200, "Ča\\/b" <(E-Mail Removed)> wrote:

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[i]=malloc(5*1024*1024);
if(s[i]==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[i]);
return 0;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Need a good web host-- 3GB bw, 1.5GB disk, mysql, perl pmlonline@gmail.com HTML 3 09-21-2005 08:13 AM
Seagate 5GB Pocket Hard Drive @ Viperlair Silverstrand Front Page News 10 07-05-2005 06:12 AM
40GB drive reports 32.5GB capacity? Bob Computer Support 7 10-13-2003 09:00 AM



Advertisments