Please give me some suggestions about malloc

  • Thread starter Thread starter nenyan
  • Start date Start date
  • Tags Tags
    Suggestions
Click For Summary
SUMMARY

The discussion centers on memory allocation issues in C programming using the malloc function. The user encounters a memory error when attempting to allocate an array of doubles with N set to 5*10^8 on a Windows 7 system with 8GB of RAM. The problem arises due to exceeding the range of a 32-bit integer, which limits the maximum allocatable memory. A suggested solution is to use a 64-bit compiler to handle larger memory allocations effectively.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with memory management concepts in C
  • Knowledge of 32-bit vs 64-bit architecture
  • Experience with the malloc and free functions in C
NEXT STEPS
  • Research the differences between 32-bit and 64-bit compilers in C
  • Learn about memory allocation limits in C on different operating systems
  • Explore alternative memory management techniques, such as using dynamic arrays
  • Investigate the use of tools like Valgrind for memory debugging in C
USEFUL FOR

C programmers, software developers working with memory-intensive applications, and anyone troubleshooting memory allocation issues in C on Windows systems.

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.
 

Similar threads

Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
16
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
20
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K