Print available memory in C [closed]

0

How to know how much memory is available before doing malloc ?

I would like to print the memory value that is still available to be able to be allocated, the code must run under Windows and Linux .

On a computer I think that this kind of information is not very important, but in a microcontroller knowing the memory that is still available should be very useful for future actions.

Something like GlobalMemoryStatusEx(&memInfo); , for the searches I did I did not find a method that really worked.

    
asked by anonymous 27.08.2018 / 13:56

1 answer

1

In a microcontroller you have to see his API and it will not run Linux, much less Windows. But in microcontrollers it is like not using malloc() , at least in real time scenarios or very high restriction, it can even have an API. Programming for microcontrollers is different from programming for microcomputers, so I say that people tend to want to make code that runs on Windows and the microcontroller and almost always that does not make sense. Even C's standard library often has a very different and limited implementation.

Memory at the micro level is somewhat complicated, and it is not easy to preach accurately, it depends on the application's allocation strategy, library used, architecture, and other factors. You can give malloc() and change nothing, because it will not actually allocate, it will use something already previously allocated in the operating system. There will be an internal allocation. This can occur because of a mapping issue or because free() does not always release to the operating system.

In Windows you already know the correct function, in Linux it is Sysconf() .     

27.08.2018 / 17:24