Print memory available in C

Asked

Viewed 165 times

0

How to know the amount of memory available before doing the malloc()?

I would like to print the memory value that is still available in order to be allocated, the code has to run on Windows and Linux.

In a computer I think it is not very important this type of information, but in a microcontroller know the memory that is still available should be quite useful for future actions.

Something like GlobalMemoryStatusEx(&memInfo);, by the research I’ve done I haven’t found a method that actually works.

  • Who vote negative could comment to know what improve in the question, I think is a very pertinent question and that does not exist in Stackoverflowpt !

  • 1

    there is no standardized way to get this information, so I’ve been seeing in the OS in English what there are are approximate hacks...probably in a microcontroller also there must be some specific way to get this information... did not give negative

  • I’ve been looking around too, didn’t want a standardized way, just something that worked.

1 answer

1

On a microcontroller you have to see its API and it won’t run Linux, much less Windows. But on microcontrollers it’s like not using malloc(), at least in real-time or extremely constrained scenarios, you may not even have an API. Programming for microcontrollers is different from programming for microcomputers, which is why I say that people tend to want to make code that runs on Windows and the microcontroller and almost always that doesn’t make sense. Even the standard C library usually has a very different and limited implementation.

Memory at the micro level is something complicated, and it is not easy to preach accurately, it depends on the application allocation strategy, the library used, architecture, and other factors. Can give a malloc() and change nothing, because effectively there will be no allocation, it will use something previously allocated in the operating system. An internal allocation will occur. This may occur because of a mapping issue or because the free() does not always release to the operating system.

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

  • I don’t know how to use the function I mentioned, I saw this function on a website without explaining how it worked, I did some research but I don’t know how to use it. I don’t know if I am printing a random value or I am printing even the available memory space, because I’m not sure.

  • Your question does not mention it and then it becomes broad, I answered because it seemed conceptual, if it was not what I wanted to know, at least it is not clear. This can occur because you have a tendency to put a lot of information irrelevant to the question and then spend little on the real doubt, makes people understand something else. As this is often the case, I will prefer to close your questions as unclear until it is clear what you really want to know and not what is written. Write on the question what you want to know, don’t write other things.

  • But I asked the central question at the beginning of the text, just this question for people to answer, then I separated it by a linha and made a summary of what I expected. Is it a broad question? In the text I wrote there is only ONE question, which is the question I want to know.

  • I spoke about the microcontroller to say that it would be pertinent to print this value, this clearly was not a question, nor was it the central question. Incidentally, I put the bold that was on Windows and Linux. If a microcontroller does not run linux or windows then it has no way to respond accordingly. The microcontroller could be spoken at the end of someone’s answer, it was an option.

  • 1

    And how are we to guess that it is central if most of the text is about something else? And if it is, it remains unclear or broad. In the yellow block on the side there is a question called "What to do with questions in the style "How to do X?"" Read it. Hence the question becomes broad, without context, without a code, without a specific doubt it becomes complicated to give an answer that is correct, would be a kick, as I said before. I even replied because she seemed more conceptual, she seemed to be about a Overview, I said "on top". Victor has a very good comment that I’m not going to think he says anything:

  • 2

    If you ask about how to build a house, the most you’ll have to answer is that you need to build brick walls and make a finish. If you ask why the wall you made "this way" is crooked and how to solve it, or why the dough is soft, how well you fix the brick on the wall, then you will have a more specific answer. Ah, I answered for Linux and Windows, in general, as the question was asked. When you try to understand the site and stop fighting it you will do better.

  • I went to research some more and ironically found in Stackoverflowen my question, the central question was exactly the same as mine and @Travis Gockel’s answer was exactly what I wanted, as well as other answers. I’m not trying to fight, just wish I knew a function with the goal I intended to later implement. I probably should have looked more instead of asking right away, but then I could help others, as happened at Stackoverflowen

  • 1

    This isn’t Soen, and the question is far from the same, it’s on the same subject, there it’s much more objective and even it provoked an answer that if I posted here you would complain that you have nothing with what you asked, and in fact the question there is very bad and should be closed. It does not justify to err here too. Everything you turn into a discussion forum. It’s already good, I can’t explain everything to you all the time, it’s not appropriate, the questions need to be good for you alone, if you need to debate all the questions you ask it gets very unproductive and you monopolize a lot of energy

  • You change the question after it is asked can not, so invalidates what I answered. I will repeat again. Here is not a forum. You ask a question and expect an answer. You can use the comments for some quick clarification, but if you need to discuss the subject, if you need to change the question after answered the question has problems and so it is closed. Even edited you can not know what you want without seeing the comments posted later. And it is still wide only asking for the code.

  • At the time I edited it was to see if it could reopen again, but I realized that the question is wide. Thanks anyway

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.