Thread: Memory allocated

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    New Delhi,India
    Posts
    17

    Memory allocated

    How to know how much of memory has been allocated in stack and how much amount of memory is free??

  2. #2
    Registered User
    Join Date
    Apr 2008
    Location
    New Delhi,India
    Posts
    17

    Null pointer

    Null pointer points to 0 or (void*)0?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by saswatdash83 View Post
    Null pointer points to 0 or (void*)0?
    Those are essentially equivalent. 0 is the value of NULL, the (void *) makes it compatible with pointers. Note however that this is C. In C++ 0 is compatible with all pointers.

    [Note also that SOME systems may internally represent NULL as a different value, but it will still be false/zero when using such a pointer in a condition].

    Quote Originally Posted by saswatdash83 View Post
    How to know how much of memory has been allocated in stack and how much amount of memory is free??
    First of all, those are two different measures. Only certain systems (particularly DOS) would use "the memory between top of stack and end of data is heap" - in fact, even there's nothing saying that it HAS to be that way.

    Second, there's no real good way to determine how much memory is free at any given time - you may be able to ask the OS, but even if you are given a number back from the OS, there's absolutely no guarantee that this number holds true when you get it back from the OS, never mind later on. Modern OS's are multitasking/multithreading, and another task may run at any given time, so what was free two microseconds ago, isn't now. Just like phoning the cinema and asking if there are tickets available for the latest movie - if they say yes, doesn't guarantee that tickets will be available when you get there. The only way to ensure that memory is available is to try to allocate it - like booking the tickets over the phone at the cinema - if it succeeds, then you have the memory, and you can use it. If it fails, then you are out of memory - do something else [usually stop, hopefully you can save what was going on without too much problem].

    --
    Mats
    Last edited by matsp; 07-15-2008 at 02:34 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Check if there is memory allocated
    By The Wazaa in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2006, 05:48 AM
  3. [C++/WinAPI] Checking number of allocated memory
    By jagi in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2005, 06:10 PM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  5. deleting dynamically allocated memory...
    By heljy in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2002, 11:40 AM