0

I haven't used C++ in quite a few years and I never properly learned how memory allocation and garbage collection works. I've been looking at videos and trying to find sources to learn from, but I've been given an instruction in an assignment and I'm not sure how to interpret what I need to do.

The instruction is "If there is not enough memory left to create a new object, the function should return false; otherwise the function returns true"

Does this imply I should be using malloc() or is there another method of checking if memory is full?

Also if anyone knows of a good guide to this topic...

  • 1
    *If there is not enough memory left to create a new object* -- Then there is no guarantee your program can do anything useful, including error reporting. – PaulMcKenzie Sep 02 '19 at 23:37
  • 5
    In C++ you should probably use `std::list` for a linked list. – Chris Dodd Sep 02 '19 at 23:40
  • 1
    *If there is not enough memory left to create a new object, the function should return false; otherwise the function returns true* sounds like you may be learning C, not C++. Check with the assigner if you are permitted to use C++-idioms. In general though, you don't want to use `new` or `malloc`. See [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) for details. – user4581301 Sep 02 '19 at 23:48
  • 1
    Sidenote: C++ doesn't normally use garbage collection. Instead it favours Automatic allocation where possible and [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) where it is not. – user4581301 Sep 02 '19 at 23:50

2 Answers2

2

In C++, you should generally not be using malloc; it's more of a C function that doesn't mesh as well with C++ paradigms.

Instead, you should be using C++'s own techniques to manage memory, such as new and delete. The upside to new over malloc is that new will correctly initialize your object with the help of your constructor, rather than simply giving you a region of memory in which you need to carefully construct an object in-place. Likewise, delete will properly call your object's destructor (while free will not).

Assuming your linked list node class/struct is called Node, the expression new Node will allocate memory for a node, and return a pointer to that node; if memory is unavailable, a best-effort attempt will be made to throw an exception (std::bad_alloc).

You might want to try to catch it, but if memory is scarce there might not be much you can do anyway given that useful operations such as printing an error message might themselves require memory to be allocated.

Holger
  • 243,335
  • 30
  • 362
  • 661
nanofarad
  • 36,174
  • 4
  • 79
  • 104
0

Jumping into memory allocation is not the primary approach in modern C++. We generally tend to use std containers(vector, list, map...) or std smart pointers- as the primary solution; If that is not applicable and/or a custom container smart pointer is needed, there is the std::allocator family of classes to be considered. Only the last option would be new/delete operators. As you've been reading this post down to here, there has been a gradually increasing risk of memory corruption; If you dig further down, the seriously hazardous C API and malloc/free family of functions appear, but this one is well off the limits and almost not used unless in very extreme cases.

Red.Wave
  • 1,427
  • 6
  • 8