Thread: detecting available memory

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    1

    Cool detecting available memory

    Hi,

    I'm writing in C89 and I need to read structs in from file into an array.

    I thought of setting the array to half the availble memory so as to not put too much strain on the pc.

    Does anybody know how to determin hom much memory is available - not including swap.

    I have not found an answer anywhere. Any help would be apreiciated.

    Thanks

    Daniel

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why don't you get the number of records in the file, then dynamically allocate the amount of memory you need.
    If that allocation fails, you report "file too big", or you fall back into processing the file in parts.

    > Does anybody know how to determin hom much memory is available - not including swap.
    Not without detailed knowledge of your OS and compiler.
    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.

  3. #3
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    I don't know about C89
    but one way to get those is
    include sys/sysinfo.h if you have it,
    call get_avphys_pages() gives you available physical pages.
    and get_phys_page_size() which gives you page size.
    simple calculation and you have available memory in bytes.
    these should found atleast on linux environment...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  3. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM