How to find the maximum memory allocation limit in c

I want to determine what maximum memory limit I can allocate on my computer. This is the code I wrote for this task:

#include <stdio.h> #include <stdlib.h> int main() { int j; int *primes; int i ; int limit = 2147483647; primes = malloc(sizeof(int) * limit); for (i = 0; i < limit; i++) { primes[i] = 1; } return 0; } 

How to determine how much memory can be allocated without hits and trial versions? In this example, I highlighted the maximum int size. But the program crashes. How much memory is really allocated in this example?

+5
source share
4 answers

malloc() allowed to crash, in which case it returns a NULL pointer without allocating memory . It is always all or nothing . It either succeeds, or allocates a full chunk of memory of the requested size, or it fails by returning a NULL pointer without allocating one byte.

How to find out how much memory is available - it really depends on the environment: in which OS are you working, is it a 16-, 32-, 64-bit memory architecture?

For example, if you are running Windows 10, you can use the GlobalMemoryStatusEx() tool (see MSDN: GlobalMemoryStatusEx () for details).

Linux, OTOH, provides a neat sysconf() way to reverse this kind of information. See this page for more details.

Even if your OS is 64-bit, which does not necessarily mean that your application can access a certain limit. For example, the 64-bit version of Windows 7 will allow you to specify up to 8 GB of memory in the application code, even if the total virtual memory space is 16 TB.

+8
source

You are mistaken for many reasons, such as

  • You assume the maximum size (for the platform / environment) that is the least believable, less portable.
  • You assume that malloc() offers a partial allocation (up to available memory) , which again is not true.

    Citation of the C11 standard, chapter §7.22.3.4, (emphasis added)

    The malloc function returns either a null pointer or a pointer to the allocated space.

    Thus, it either completes success (with the exact amount of allocated memory) or completely fails (returning a null pointer), there are no compromises, such as partial success with available memory or something else you could assume.

  • You do not check the success of malloc() , which leads to a possible dereferencing of the null pointer.

  • You do not have the means to actually verify success / failure in distribution

I believe you need getrlimit() and a family to achieve your goal. A particular point of interest will be RLIMIT_DATA as the value of resource .

Nonetheless,

1. "In this example, I have selected the maximum size int"

It seems that this is not related to the restrictions for malloc() and the family.

2. "But the program will work"

This is most likely the result of undefined behavior . In your code, you are directly looking for the returned pointer without checking for success with malloc() . It is likely that malloc() failed and returned NULL , the dereferencing that calls UB.

+4
source

The maximum amount of memory that you can allocate is controlled by several factors and can change over time.

These include: 1. Hardware limits 2. OS restrictions 3. System parameters 4. Process quotas 5. Page file space

In addition, malloc is a very poor way to allocate large blocks of memory.

Your program crashes because malloc returns null, and you use this return value without checking.

0
source

There are so many loop iterations you create, these are the main reasons your program crashes or the loop is infinitely closed.

The answer to what you expect to find out is very complicated due to some key notes ---- →

1 .. It depends on the platform on which the program runs. windows, linux or mac. I THINK that the amount of memory is not limited by anything other than physical memory.

Fact → Although physical memory can be expanded with virtual memory, not all platforms have a “virtual memory” function. C has no concept of virtual memory. Malloc allocates continuous memory (i.e. side by side or together in ram).
Thus, it depends on how the platform processes the request. It depends on the implementation of C.

2. The largest number (in bytes) represented by the standard type 'size_t' (declared). This value can and depends on the implementation. Note that this value is not necessarily as large as the available memory of the host platform ( i.e. End> ).

QUES. Are there any restrictions on this? Where should I get this kind of information?

Ans . The argument of Malloc is size_t, and the range of this type is [0, SIZE_MAX], so the maximum you can request is SIZE_MAX, the value varies from implementation to implementation and is defined in.

Note. - Whether the request for SIZE_MAX bytes will be successful depends on factors outside the scope of this group.

-1
source

Source: https://habr.com/ru/post/1264234/


All Articles