-1

I'm struggling with debugging a big c++ program that goes out of memory after a few hours, does any of those scenarios create a memory leak ?

Scenario 1 : The use of & (without delete or anything like this) :

inline int testFunc(std::vector<int>& peaksArray, int& tmp) {
for (int Index : peaksArray) 
{
tmp++
}
}

Scenario 2 : the use of char[] (without delete or anything like this) :

char debug[500];    

I NEVER use in my program :

  • new
  • delcaration with *
  • delete (maybe I should ?)

Thanks for your answers,

Regards

user
  • 934
  • 6
  • 17
Francois
  • 93
  • 1
  • 15
  • Is that all your code does? I mean, any other sources for the memory leak? may be is just that your vector (or any other container) is actually becoming too large? – cbuchart Mar 06 '17 at 23:50
  • The code you've posted does not leak memory. – GigaRohan Mar 06 '17 at 23:52
  • A memory leak is when memory gets allocated and then never deallocated. Do any of the above things cause memory to get allocated and then never deallocated? – user253751 Mar 06 '17 at 23:54
  • You may use more and more resource without leak, or you may have fragmentation for your out of memory observation. – Jarod42 Mar 06 '17 at 23:55

2 Answers2

6

If you don't use new, don't use delete. Local variables have a default lifetime of the scope in which they're declared. Once that scope ends, attempts to use that variable result in undefined behavior.

JGroven
  • 613
  • 1
  • 9
  • 14
1

Seems like you do understand it - with one exception: In your example, debug is a stack variable like everything else. new or malloc create on the heap, everything else (local variables etc) is on the stack. And main's local variables are not different from any other function's variables.

Shared memory is a rather rare case, you usually don't need it and therefore you won't have it unless you explicitly ask for it (otherwise, some random another process may use the very same memory your process uses - obviously, this would break things badly).

For more information please take a look at here.

Community
  • 1
  • 1
Sam Mokari
  • 461
  • 3
  • 11
  • [Recommend brushing up on the terminology.](http://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and) – user4581301 Mar 07 '17 at 00:21