When trying to cut down possible memory leaks in my program, I noticed a reoccoring pattern: Move Delete closer to new, repeat, remOve the function that uses it and move code to main, move a few things to global, replace some stuff with arguments and return values, and in the end everything is in main with no pointers.
However to reassure myself that this wont have problems I want to know this : If my program doesnt use the stack or heap at all (as in me putting a new or stk()? in it) will it still have (however small) memory leaks? It was made to work forever constantly recalculating it's output every second, evaluating the time, year, day, ticks since start, CPU speed (<<in progress), so basically if you open this in DOS your doomed.

I said I don't use ANY pointers, heap, or stack well it does use NULL whitch I think is a pointer to zero.., I just want to make sure that the program can keep running without me having to turn the computer off.

If my program doesnt use the stack or heap at all (as in me putting a new or stk()? in it) will it still have (however small) memory leaks?

If you don't create leaks, or use any libraries that create leaks, then it stands to reason that there won't be any leaks. ;)

You should just run your program in Valgrind and see the report afterwards about if there are any memory leaks. Or, use any other memory debugging tool.

Basically, memory leaks are a consequence of heap-allocated memory (or freestore in general) that is not freed (and is then forgotten, made unreachable), put simply, not all new-calls are matched with a delete-call. Other forms of memory cannot cause leaks (e.g. the stack or global variables) simply because the compiler generates automatic code to free stack memory, and the OS does the rest.

Surely, not using the heap at all is pretty much guaranteed not to allow for memory leaks. But that is a high price to pay. All you need to do to avoid memory leaks is a little discipline at doing two things: create RAII classes, and use smart-pointers. My tutorials might be of interest in that regard (RAII and Ownership).

Btw, memory leaks aren't the only thing to worry about if you want your program to be able to run forever. You also need to make sure that your stack is not growing indefinitely. In other words, you cannot base your main loops on recursion.

Recursion is a function calling itself. This will show you recursion. If you look above that post you will se an iterative version of the same code.

Edited 4 Years Ago by NathanOliver: n/a

Woo what a relief , its a do while loop with the condition being 0==0. Would that tear it down?

Why use while(0==0) when you could just use while(true) ? Furthermore, why would you use a do-while loop when your condition is infinite?

This article has been dead for over six months. Start a new discussion instead.