473,761 Members | 7,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to malloc 2G ram? in freebsd, win2k

hi
i try to malloc 2G size mem . so i wirte code like that.
it failt in freebsd 5.3 and win2k

what's bug in my code ? how can i change it ?

thank all :)
benjiam
///////////////////////////////////////////////
#include <iostream>
using namespace std;

#include <cstdlib>
const int len = 2000;
int main()
{
char* p1[len];
static int size = int (1<<20);
cout << size << endl;
for (int i =0 ; i < len ;i++)
{
p1[i] = new char [size];
cout << size << endl;
char *tp = p1[i];
for ( int x =0 ; x< (size-1); x++)
{
//cout << x << endl;
*(tp+x) = char( (x%26)+ 48);

}
*(tp+size-1) =0;
cout << "p [" << i << "]ok" << endl;
}

for (int i =0 ; i < len ;i++)
{
delete [] p1[i];
}
return 0;
}
////////////////////////////////////////////
Nov 14 '05
23 3025
hi
I _think_ Linux gives you 3G, but I could be wrong. I don't know if
these limits persist to AMD-64 or not. However, I would try it on a
64-bit PowerPC -- it should work there.

u run my code? :)

#include <stdio.h>
#include <stdlib.h>
const int len = 2000;
int main()
{
char* p1[len];
int i =0;
int x =0;
static int size = 1<<20;
printf( "%d \n", size);
for ( i =0 ; i < len ;i++)
{
p1[i] = (char*) malloc (size);
printf( "%d \n", size );
char *tp = p1[i];
for ( x =0 ; x< (size-1); x++)
{
//cout << x << endl;
*(tp+x) = (char)( (x%26)+ 48);

}
*(tp+size-1) =0;
printf( "p [ %d ] ok\n" , i);
}

for ( i =0 ; i < len ;i++)
{
free(p1[i]);
}
return 0;
}







"Jonathan Bartlett" <jo*****@eskimo .com>
??????:42****** **@news.tulsaco nnect.com... ch***********@h otmail.com wrote:
hi
i try to malloc 2G size mem . so i wirte code like that.
it failt in freebsd 5.3 and win2k

what's bug in my code ? how can i change it ?


I don't know about FreeBSD, but on Windows a process cannot be bigger
than 2G, even if you have more RAM than that. Each process is limitted
to at most 2G. So, if you add the space for your code, libraries, and
existing variables, malloc'ing 2G on a Windows box is guaranteed to fail.

I _think_ Linux gives you 3G, but I could be wrong. I don't know if
these limits persist to AMD-64 or not. However, I would try it on a
64-bit PowerPC -- it should work there.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017

Nov 14 '05 #11
On Tue, 22 Mar 2005 18:57:26 GMT, CBFalconer <cb********@yah oo.com> wrote:
Raymond Martineau wrote:
<ch***********@ hotmail.com> wrote:

... snip ...
p1[i] = (char*) malloc (size);


This causes undefined behaviour when malloc returns NULL. Check
the return value.


No it doesn't. NULL is a perfectly valid value for a char*.
However the useless cast can conceal errors, and should be avoided.


Actually, I was referring to the fact that there wasn't any checking on the
return value (which was later blindly used.)

In this case, I didn'tpoint out the line at which it would fail.

Nov 14 '05 #12
In article <d1**********@n ews.yaako.com>, <ch***********@ hotmail.com> wrote:
:I _think_ Linux gives you 3G, but I could be wrong.

Something I read awhile ago suggested that it depended on the kernel
version and how you compiled.

:u run my code? :)

After some cleanup and optimization, yes. Compiled as a 32 bit
executable, it runs on one machine until it pretty much reaches the
end of the storage space "underneath " the virtual address that libc
gets placed at. Compiled as a 64 bit executable and run on a different
machine, it run to completion -- the object layout is different for
the 64 bit executables.
--
Any sufficiently advanced bug is indistinguishab le from a feature.
-- Rich Kulawiec
Nov 14 '05 #13
<ch***********@ hotmail.com> wrote in message
news:d1******** **@news.yaako.c om...
sorry! i am not prepense
i write it in c , and comp it under win2k and freebsd5.3

in freebsd
p [ 294 ] ok
1048576
Killed
There is no guarantee that a program can allocate arbitrarily large amounts
of memory. malloc() is allowed to return NULL at any time.

<OT>
Virtual memory doesn't mean that you can allocate 2-4GB in each application;
there is almost certainly a limit that your OS or configuration imposes
below that. Apparently your system's limit is 294MB for this program.
</OT>
for ( i =0 ; i < len ;i++)
{
p1[i] = (char*) malloc (size);
The cast is unnecessary (and not idiomatic).
printf( "%d \n", size );
char *tp = p1[i];
for ( x =0 ; x< (size-1); x++)
{
//cout << x << endl;
*(tp+x) = (char)( (x%26)+ 48);
You forgot to check if p1[i], and thus tp, is NULL; dereferencing that'll
ruin your day.

}
*(tp+size-1) =0;
printf( "p [ %d ] ok\n" , i);
}

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #14
ch***********@h otmail.com wrote:
hi


<mowed>

Please do not top-post in c.l.c.

Regards,
Jonathan.

--
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

"I usually swear at C++, but so far it has never sworn back."
- Ben "Noir"
Nov 14 '05 #15
In article <d1**********@c anopus.cc.umani toba.ca>, ro******@ibd.nr c-
cnrc.gc.ca says...
In article <d1**********@n ews.yaako.com>, <ch***********@ hotmail.com> wrote:
:I _think_ Linux gives you 3G, but I could be wrong.

Something I read awhile ago suggested that it depended on the kernel
version and how you compiled.
OT: This can be done under Windows also, provided you have less than
4GB of RAM total and do not have /PAE turned on. Google for boot.ini
options.
After some cleanup and optimization, yes. Compiled as a 32 bit
executable, it runs on one machine until it pretty much reaches the
end of the storage space "underneath " the virtual address that libc
gets placed at.
I think that you will discover that for a system running 32-bit
Windows or Linux with 2GB of RAM or more installed, and using default
kernel options (I.e. no 3GB address space hack), that a reasonably
sized command line app will be able to malloc around 1600-1700MB of
RAM prior to it failing. People that claim that malloc() never fails
should try this for themselves.
Compiled as a 64 bit executable and run on a different machine, it
run to completion -- the object layout is different for the 64 bit
executables.


Malloc will also fail (eventually) on Linux/glibc 64-bit
combinations. The point it fails varies. I tried this a while back
on an x86_64 w/64GB (not a typo) of RAM. I forgot where it failed
in a single process (16GB?), but recall that with pthreads (NPTL),
it would fail at just under 8GB/thread, but you could effectively
chew up the entire 64GB by starting 8 threads and having each ask
for 8GB.

I haven't tried this on a Win64 x86_64 system (yet).

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #16
For one thing, you need a systen capable
of allocating over 2 Gigs of memory, and
that could be a bit challenging to an average
PC.

Next, ever heard of a NULL pointer?

BTW, I run it on my sytem with a target changed to
3 Gig, and with some extra diag code put in there,
and it did go to 2.1 Gig.

(Said PC has 2 Gigs of physical memory, BTW)

<ch***********@ hotmail.com> wrote in message
news:d1******** **@news.yaako.c om...
sorry! i am not prepense
i write it in c , and comp it under win2k and freebsd5.3

in freebsd
p [ 294 ] ok
1048576
Killed

Nov 14 '05 #17
hi
thanks for your help , and i change it again.
code i fixed
///////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>

const int len = 800;
int main()
{
char* p1[len];
int i =0;
int x =0;
char *tp;
static int size = 1<<20;
printf( "%d \n", size);
for ( i =0 ; i < len ;i++)
{
p1[i] = (char*) malloc (size);

tp = p1[i];
if (tp == NULL)
continue;
printf( "%d \n", size );

for ( x =0 ; x< (size-1); x++)
{
//cout << x << endl;
*(tp+x) = (char)( (x%26)+ 48);

}
*(tp+size-1) =0;
printf( "p [ %d ] ok\n" , i);

}

for ( i =0 ; i < len ;i++
)
{
if (p1[i] == NULL)
continue;
free(p1[i]);
printf("i clean %d \n", i);
}
return 0;
}

///////////////////////

in win2k it can run success,
but in linux and freebsd it fail. killed by os

i wish if malloc() fail, it can continue, in win2k it success
in linux and freebsd it fail.

benjiam







"Raymond Martineau" <bk***@ncf.ca >
??????:kn****** *************** ***********@4ax .com...
On Tue, 22 Mar 2005 18:57:26 GMT, CBFalconer <cb********@yah oo.com> wrote:
Raymond Martineau wrote:
<ch***********@ hotmail.com> wrote:
... snip ...

p1[i] = (char*) malloc (size);

This causes undefined behaviour when malloc returns NULL. Check
the return value.


No it doesn't. NULL is a perfectly valid value for a char*.
However the useless cast can conceal errors, and should be avoided.


Actually, I was referring to the fact that there wasn't any checking on

the return value (which was later blindly used.)

In this case, I didn'tpoint out the line at which it would fail.

Nov 14 '05 #18
In article <d1**********@n ews.yaako.com>, <ch***********@ hotmail.com> wrote:
thanks for your help , and i change it again. code i fixed
p1[i] = (char*) malloc (size);
tp = p1[i];
if (tp == NULL)
continue;


You do not want a 'continue' there, you want a 'break'.
Otherwise it will loop around again, and fail the malloc
again.

The version below is a bit more efficient.

#include <stdio.h>
#include <stdlib.h>
const int len = 2000;
const int size = 1<<20;
int main()
{
char* p1[len];
char p2[size];
int i =0;
int x =0;
for ( x = 0; x < (size-1); x++ ) p2[x] = (char)((x%26) + 48 );
p2[size-1] = 0;

printf( "%d \n", size);
for ( i =0 ; i < len ;i++) {
p1[i] = malloc (size);
if ( !p1[i] ) break;
strncpy( p1[i], p2, size );
printf( "p [ %d ] ok\n" , i);
}

while (--i >= 0) free( p1[i] );

return 0;
}
I haven't had a chance yet to try it out compiled for 64 bit pointers
on a system with 1.5 Gb physical memory.
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler
Nov 14 '05 #19
i suppose
in case of i =90
p1[i] = (char*) malloc (size);
tp = p1[i];
if (tp == NULL)
continue;
it fail
loop
i= 91

it fail
loop
i =92
at this time ,the other process free a lot of memory
and this time it will malloc success.(maybe it is wrong idea,resource
cortroled by os)

You do not want a 'continue' there, you want a 'break'.
Otherwise it will loop around again, and fail the malloc
again.
"Walter Roberson" <ro******@ibd.n rc-cnrc.gc.ca> дÈëÏûÏ¢ÐÂÎÅ
:d1**********@c anopus.cc.umani toba.ca...
In article <d1**********@n ews.yaako.com>, <ch***********@ hotmail.com>

wrote:
thanks for your help , and i change it again.

code i fixed
p1[i] = (char*) malloc (size);
tp = p1[i];
if (tp == NULL)
continue;


You do not want a 'continue' there, you want a 'break'.
Otherwise it will loop around again, and fail the malloc
again.

The version below is a bit more efficient.

#include <stdio.h>
#include <stdlib.h>
const int len = 2000;
const int size = 1<<20;
int main()
{
char* p1[len];
char p2[size];
int i =0;
int x =0;
for ( x = 0; x < (size-1); x++ ) p2[x] = (char)((x%26) + 48 );
p2[size-1] = 0;

printf( "%d \n", size);
for ( i =0 ; i < len ;i++) {
p1[i] = malloc (size);
if ( !p1[i] ) break;
strncpy( p1[i], p2, size );
printf( "p [ %d ] ok\n" , i);
}

while (--i >= 0) free( p1[i] );

return 0;
}
I haven't had a chance yet to try it out compiled for 64 bit pointers
on a system with 1.5 Gb physical memory.
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler

Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2317
by: pembed2003 | last post by:
Hi, If I have the folllowing: char* p = malloc(5); memset(p,-1,5); *p = 0; printf("%d\n",strlen(p)); free(p); It will print 0. Is there a way to retrive the initial size of memory
2
1432
by: dsptechie | last post by:
I wanted to how exactly malloc() function works. I came to know that malloc() allocates memory only in powers of 2. i.e if asked for say 17 bytes , in the process, it allocates 32 bytes and returns 17 bytes so 15 bytes are wasted... Is this true...If anybody knows the algorithm details of malloc() plase share the same. Is this one of the reason why malloc() is not generally preferred in embedded systems..?
40
540
by: ramu | last post by:
Hi, what happens when i run the below code? main() { int *p; while(1) p= (int *)malloc(1000); } Do i get segmentation fault?
37
3202
by: ravi.cs.2001 | last post by:
Hi all, I m relatively new to C. I have few queries related to malloc(): #1. When we perform malloc(), the memory allocated dynamically comes from the heap area of the process in concern. Well, we then say that the heap has shrinked. my query is: Is it that the heap physically does not shrink but the perticular nodes are marked 'ALLOCATED' and for subsequent calls to malloc() the memory manager remembers them and does not reference...
34
13406
by: niranjan.singh | last post by:
This is regarding to test an SDK memory stuff. In what situation malloc gets fail. any comment/reply pls.... regards
6
3371
by: itsolution | last post by:
Hi folks, Could you shed some light on this issue? my program is running on Freebsd as a daemon. When user sends a request, it forks itself and lets its child process handles the request. And its main role is just to read a big xml file and save each object into its embedded DB(such as gdbm).
11
1684
by: pushpakulkar | last post by:
Hi all, Many users claim that malloc() and free() provided by the language library results in considerable amount of overhead in terms of calling the same. Can we really eliminate overhead by using used defined functions/user defined memory manager. Is there any significant advantage in providing user defined malloc() and free(). I want to exclude realtime systems or embedded systems which are memory constrained here.
0
1954
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see math, mmap and readline modules, that worked fine before, cannot be built anymore.
0
1814
by: M.-A. Lemburg | last post by:
On 2008-10-25 20:19, Akira Kitada wrote: Thanks. The errors you are getting appear to be related to either some missing header files or a missing symbol definition to enable these - looking at the ticket, you seem to have resolved all this already :-)
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.