Please give me some suggestions about malloc

  • Thread starter Thread starter nenyan
  • Start date Start date
  • Tags Tags
    Suggestions
AI Thread Summary
The discussion centers on a C program that attempts to allocate a large array of doubles, specifically when the size exceeds 250 million elements. Users report encountering a memory error when trying to allocate more than 2.5 * 10^8 due to limitations in memory allocation on a 32-bit system. The program runs on Windows 7 with 8GB of RAM, and it is suggested that using a 64-bit compiler could resolve the issue, as it would allow for larger memory allocation. The problem is attributed to exceeding the range of a 32-bit integer, which restricts the maximum allocatable memory size.
nenyan
Messages
67
Reaction score
0
Code:
#include <stdio.h>
#include <stdlib.h>
#define N 250000000

int main()
{
    int i; 
    double *x;

    if(!(x = malloc(N * sizeof(double)) ))
            printf("memory error \n");

free(x);
}
When N is larger than 2.5*10^8, it appears memory error. I have 8GB memory. The system is Windows7. How to solve this problem. I hope N=5*10^8 at least.
 
Technology news on Phys.org
A 64-bits compiler will probably help.
As it is you are overrunning the range of a 32-bits integer.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top