Thread: ???get max memory before allocate memory?

  1. #1
    Registered User
    Join Date
    Apr 2019

    Question ???get max memory before allocate memory?


    checked memory allocate,its possible to allocate memory with nocatch and it leaves NULL pointer,(64bit with many gigabytes)
    but there must be a better way than allocate much memory as possible than a loop that trials and errors true allocate with nocatch
    like get maxmem and subtract a number of bytes to avoid OS getting unstable,how much?half a gig?1 gig?
    you tell me you can C,why dont you C your own bugs?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Perhaps you should consider using a more sophisticated algorithm such that you can avoid excessive memory usage, or perhaps it may be a good idea to store not currently used data to disk?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well for one thing, it's sure to depend on your OS of the moment.

    For another thing, the amount of memory you could theoretically allocate at any given moment is likely to be a fluid number that depends on whatever else is going on.

    Between asking 'can I allocate X bytes' and then requesting 'give me X bytes', it can all change.

    The XY Problem
    Tell us the problem, not how to work around your solution.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2017
    You can allocate more memory than you have physical RAM. The memory that is allocated is "virtual memory" and can in fact exist mostly on disk. This can then be very slow to use, of course, but depending on the algorithm (more specifically, the access pattern) it can work. Still, it's generally something you try to avoid, except perhaps for the case of a memory-mapped file.

    Depending on what you are actually trying to do, it may be reasonable to use an OS function to determine how much actual RAM you have and then to request some large fraction of that. As long as you know that your process is the only memory hog on the system then it can work out okay.

    To check for total/available memory on Windows: GlobalMemoryStatusEx function (sysinfoapi.h) - Win32 apps | Microsoft Docs

    if ( GlobalMemoryStatusEx( &mem ) )
        cout << "TotalPhys   : " << setw(12) << mem.ullTotalPhys    << '\n';
        cout << "AvailPhys   : " << setw(12) << mem.ullAvailPhys    << '\n';
        cout << "TotalVirtual: " << setw(12) << mem.ullTotalVirtual << '\n';
        cout << "AvailVirtual: " << setw(12) << mem.ullAvailVirtual << '\n';
        cerr << "GlobalMemoryStatusEx failed.\n";
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  5. #5
    Registered User
    Join Date
    Apr 2019


    thanks John.c, that what I was looking for,and only want use max physical,but try learn take advantage of 64bit strength is to use lots of fast RAM and if not much memory aviable ,fall back on smaller allocate with or without use slower file read/write/append
    you tell me you can C,why dont you C your own bugs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  3. Allocate memory for an Array
    By Coding in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2008, 06:18 PM
  4. Allocate memory program
    By Jenica in forum C++ Programming
    Replies: 0
    Last Post: 11-19-2004, 01:05 PM
  5. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 11:40 PM

Tags for this Thread