C programming language
 
Question:
Avatar

How can I find out, through a C prog, how much memory is available?

By Robert Asked Jun 28 2009 7:25PM
8
Pts
 
 
Rate Question
Answer Question Help someone!
Get the latest questions in C programming language
flag

Welcome to Answerbag, a community of people sharing what they know.
Sign up now to ask a question or help someone else by giving an answer!

signup now
Sort answers by: Rating | DateArrow Down
 

Top Answer out of 3

by vittau on Jul 15, 2009 at 10:11 pm Permalink

Avatar
You can make a for loop that will try to malloc an increasingly larger chunk of memory (and then free), and when it fails, this is how much memory you have.
There surely is a better (and faster) way, but this is the only way I know...

Oh and this method will count the virtual memory also...
5
Pts
 
 
Rate Answer
 
flag
Comments (be the first to comment)

Answer 2 out of 3

by POP Fan on Nov 18, 2009 at 12:55 pm Permalink

Avatar
You could try making a pointer and starting it at a position, say 0x0100, the starting point for .COM applications. You could continually increment this pointer, incrementing a counter variable for each location that the program is allowed to access. I'm not sure exactly how well this would work though, you'd likely crash the program trying to access a memory location outside of the correct segment, but the other two answers covered the best options.

I think there was a function to find the number of bytes of free memory a computer has, but I don't remember it. It might be part of the Windows API.
0
Pts
 
 
Rate Answer
 
flag
Comments (be the first to comment)

Answer 3 out of 3

by yeroco on Jul 22, 2009 at 1:09 am Permalink

Avatar
If you are on linux, you can read and parse /proc/meminfo.
0
Pts
 
 
Rate Answer
 
flag
Comments (be the first to comment)


Add an Answer

How can I find out, through a C prog, how much memory is available?

How to write a good answer
Your answer:

Display answer in fixed-width font (good for tables or text diagrams)

Answers must adhere to our Terms of Use

To create links, just type the address with no HTML code. Use the Preview button at the bottom to verify.

You can edit your answer at any time.

Add Video Add Iimage


Important: Answerbag cannot guarantee the accuracy of answers submitted by members, and we recommend that you use common sense when following any advice found here. Read full disclaimer.