-2

When I was making a linked list in C++ for my university course, my professor told us to just use:

bool Stack::full() const{
   return false; //Assume infinite memory
}

For our linked list implementations of the data structures we made in the class (stacks, lists, queues).

If you were to push something on to the stack and the stack is using the maximum amount of memory it can, this would cause the application to crash or do something weird right?

Is there a way to check that the linked list is full/out of memory? Or is it usually just assumed that linked lists will not grow to the size where they run out of memory?

  • Possible duplicate of [How to determine CPU and memory consumption from inside a process?](http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process) – SegFault Jul 22 '16 at 05:25
  • 1
    On a system with virtual memory, like all desktop systems today, what happens when a process' memory usage exceeds the size of physical memory is that things go **slower**. And slower. And sloooooooower. You probably never get to the limit of having used up all available address space, even on a 32-bit system. Long before that the unfortunate system effectively grinds to a halt, producing weird whining noises from its harddisk. – Cheers and hth. - Alf Jul 22 '16 at 05:27
  • Okay, so I suppose it is usually just perfectly okay to say a linked list is never full unless you explicitly need to just limit it for other reasons. Thanks! – Nathan Yocum Jul 22 '16 at 05:31
  • Or you have reasons to believe you will have vast memory requirements or itty-bitty living space. Why when I was a lad we had half a K of RAM and we LIKED it that way! – user4581301 Jul 22 '16 at 05:36

2 Answers2

2

"Out of memory" is a very hard problem to tackle... It is assumed that once you get out of memory, you will not have any memory left to handle the error.

Other than that: either you set a top limit to the number of elements yourself (a healthy thing to do!), or the system will warn you by failing to allocate some memory.

The standard library containers already do so, and will check this allocation failure, and will throw a bad_alloc exception.

That's why your professor should also have told you to never write stacks, vectors and lists yourself, but rather use the ones in the STL, and start solving real problems :)

xtofl
  • 40,723
  • 12
  • 105
  • 192
  • "That's why your professor should also have told you to never write stacks, vectors and lists yourself". Heavily disagreed here. Write each of these at least once in your life as an educational venture. It's rare that you should just use some standard solution without at least knowing how that solution works. – Taywee Jul 23 '16 at 07:56
0

first allocate memory using malloc() function to particular node, if malloc() function returns NULL value then memory is full.