473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

malloc problem

Hi All,

I am trying to use malloc function to allocate memory for 580,000,000 numbers (long int).

valid = 580000000;
infoBuffer = malloc((valid)*sizeof(long));

if(!infoBuffer)
printf("Allocating memory failed\n");
else
printf("Successful memory allocation\n");

I have all these numbers in a file.

I am using my C code to load these numbers to the buffer so that I can use them to calculate something.

The memory allocation fails, but when I lowered the number to 520,000,000 it successfully loads them to memory, which tells me the limitation is 520,000,000. (i tried 521,000,000, it fails)

I tried this on a 4GB RAM machine and also on a 64 bit machine but all in vain (limitation 520,000,000 for all of them).
By the way my machine is 1GB.

I am using Visual C++.

Is this the limitation of VC++? or the malloc() ? Anything else?

Please advise me on any other function I can use to get this done.
Feb 19 '08 #1
8 1437
weaknessforcats
9,208 Expert Mod 8TB
There arte two questions I have:
1)Why do you need all 580 million longs in the machine at once???
It looks like you may have a design weakness.

2) Why are you not using SQL Server or Oracle where these problems have already been solved and coded???
Feb 19 '08 #2
There arte two questions I have:
1)Why do you need all 580 million longs in the machine at once???
It looks like you may have a design weakness.

2) Why are you not using SQL Server or Oracle where these problems have already been solved and coded???
Each of the number is a node and it carries vital information (when decoded). Given any two nodes, I should connect those nodes by finding the "in-between" link nodes (which are again in these 580M numbers).

I cannot use SQL or Oracle as these are not supported by the client.
Feb 19 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Then you will need to design differently. Probably, you will use a disc file precisely formatted so the 580 million longs are divided into manageable sections that can be floated in and out of memory.

This will be a huge coding effort where you re-create code that has already been written. I stirngly suggest you get yourself a DBMS for this.
Feb 19 '08 #4
Then you will need to design differently. Probably, you will use a disc file precisely formatted so the 580 million longs are divided into manageable sections that can be floated in and out of memory.

This will be a huge coding effort where you re-create code that has already been written. I stirngly suggest you get yourself a DBMS for this.

Thank you very much for your reply.
So, is this the limitation of malloc or the computer? or VC++?
What do you think?
Feb 19 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
I expect the limit is your OS.

For example, on Windiows XP, a long has a max value of 2,147,483,647.

580 million longs needs 2,320,000,000 bytes of memory.

If malloc() attempts to use a long for the number of bytes to allocate, it will overflow that long.

This means you will need to design your code to use allocated segments. You will truly be at this for a long time. Seriously consider a DBMS making sure your table size and row limits are within your requirements.

Of course, you could go to a 64-bit OS where you have 64-bit integers.
Feb 19 '08 #6
RRick
463 Expert 256MB
I don't think the problem is with the compiler. Malloc takes a size_t parameter which is usually an unsigned int. On most 32 bit OSes this would be a 32 bit value which is plenty big for your value. If you really want to see what the max value is, then take a look at the system's malloc.h. You'll find size_t defined there, somewhere.

Also, where your program gives up is not at a 2**31 boundary. To cross that boundary, you would need 537,000,000 four byte numbers, not 521.

Soooo, it looks like a system memory issue and this is where the fun begins. Windows has a habit of using up the address space for its own uses (remember 640K?). For example, Windows 2000 could only used 2GB of memory for an application and I hear that 32 bit XP and Vista can use 3GB max. If you want to go higher you need a 64 bit system.

As for accessing that number of values, I agree with W4cats. Your are going beyond the system memory into virtual memory and that translates into slow, very slow processing (the issue is called thrashing). I would definitely suggest a design change to get around this limitation, or else get a machine big and bad enough to deal with that amount of data.
Feb 20 '08 #7
I don't think the problem is with the compiler. Malloc takes a size_t parameter which is usually an unsigned int. On most 32 bit OSes this would be a 32 bit value which is plenty big for your value. If you really want to see what the max value is, then take a look at the system's malloc.h. You'll find size_t defined there, somewhere.

Also, where your program gives up is not at a 2**31 boundary. To cross that boundary, you would need 537,000,000 four byte numbers, not 521.

Soooo, it looks like a system memory issue and this is where the fun begins. Windows has a habit of using up the address space for its own uses (remember 640K?). For example, Windows 2000 could only used 2GB of memory for an application and I hear that 32 bit XP and Vista can use 3GB max. If you want to go higher you need a 64 bit system.

As for accessing that number of values, I agree with W4cats. Your are going beyond the system memory into virtual memory and that translates into slow, very slow processing (the issue is called thrashing). I would definitely suggest a design change to get around this limitation, or else get a machine big and bad enough to deal with that amount of data.

Thank you very much Rick and Cats.

To see if this works on a 64 bit machine, I ran the .exe file in a 64 bit machine. It gives me the same error.

The reason could be that this executable was created in a 32 bit machine. Since the 64 bit machine did nt have VC++, I was nt able to create the exe in that machine.

Do you think its worth installing VC++ in the 64 bit machine and try executing this file. That machine has got 4G RAM and I feel its powerful.

Please advise.
Feb 20 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
I say again: The total array size cannot exceed 0x7fffffff, which is 2,147,483,647.

2,147,483,647 is LONG_MAX.

malloc() needs 580000000 X 4 bytes and that number does not fit in a long.

I repeat, you will have to allocate this in segments.

Truly, check out using a DBMS where these problems have already been worked out.

Using a 64-bit OS will work if the long is 64-bits. So, you could switch to 64-bit Unix or Linux. 64-bit Windows retains the sizes of the 32-bit built-in types and adds new types for 64 bits. malloc() uses the built-in types so 64-bit Windows won't work for you.
Feb 20 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
116
by: Kevin Torr | last post by:
http://www.yep-mm.com/res/soCrypt.c I have 2 malloc's in my program, and when I write the contents of them to the screen or to a file, there aren addition 4 characters. As far as I can tell,...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
8
by: Snis Pilbor | last post by:
First, let me announce that this is very possibly off-topic because malloc is a specific third party accessory to c, etc. I spent about an hour trying to find a more appropriate newsgroup and...
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.