Thread: Regarding allocating memory

  1. #1
    Registered User
    Join Date
    Jul 2012

    Regarding allocating memory

    To check if a new node can be inserted, I use dynamic memory allocation so if heap is full, then no new node returned (false returned). But if I created new node non-dynamically, it would come from the run time stack, right? But heap or run time stack, they are both just specially defined areas of memory where former requires explicity deallocation so the memory can be reused, while latter is automatic, so my ques is if I create new node non-dynamically and no memory left (which I'm sure we mean RAM), program will not compile, is that correct? Or b/c modern OS knows how to get access to secondary memory (hard drive), it'll slow down the program running, would that be correct?

    bool front_insert(sll** head, int d)//returns TRUE if enough memory to create new node to insert into list
        sll* newNode = new sll;//Q: what if I just use non-dyanmic allocated obj, then how to test if there is mem to create the new node or not?
        if ( !newNode ) return false;
        (*newNode).data = d;
        if ( !(*head) )//if empty list
            (*newNode).next = NULL;
            *head = newNode;
        else//else non-empty
            (*newNode).next = *head;
            *head = newNode;
        return true;

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Call stack - Wikipedia, the free encyclopedia
    Memory allocation does not happen at compile time at all.

    Getting something to compile that deliberately attempts to use all of the stack space is only difficult if you don't try very hard. You only have to do it without exceeding environmental limitations, which means not using too many class members, and not using huge constants to represent sizes, and things of that nature. The limits of the environment should be in a standards section somewhere. The compiler can catch those things. I'm positive the compiler can't stop you, exactly, from consuming all available stack space though. With little effort, a simple recursive function could cause stack overflow.

    Modern operating systems can swap hard drive memory into RAM space. The only reason this would happen is because the computer is running low on RAM space, so one of the simple responses would be that inactive applications would have their assets placed on the hard drive while active programs should stay in memory. It is the user's responsibility to mitigate this problem by buying more RAM. I can also tell you from personal experience that the decline in computer performance completely sucks. It usually happened when I was watching long videos online and even if I was doing nothing else the machine became borderline unresponsive. It is not a way to fake more memory than what you have.

    As a programmer, you need to sort of justify owning the machine, if that is what your program needs. You better be making a state of the art game, or curing cancer, or something important.
    Last edited by whiteflags; 08-29-2012 at 07:57 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    You're using C++, so 'new' never* returns NULL. If there is not enough memory or the construction otherwise fails, then an exception is thrown.

    *no-throw new is something else, that I wont go into.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocating memory for structure
    By baxy77bax in forum C Programming
    Replies: 2
    Last Post: 04-30-2011, 01:49 PM
  2. allocating some memory
    By supaman in forum C Programming
    Replies: 7
    Last Post: 03-09-2006, 10:34 PM
  3. allocating memory?
    By SamuraiDave in forum C Programming
    Replies: 2
    Last Post: 09-21-2005, 02:45 PM
  4. allocating memory
    By viaxd in forum C Programming
    Replies: 2
    Last Post: 10-12-2003, 07:13 PM
  5. Allocating memory...
    By PsychoBrat in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 11:09 PM