Alex Rivera | Logout

Why C/C++ have memory issue?

Asked 2010-06-05T06:17:56.053
9

I have read lots of programmers saying and writing when programming in C/C++ there are lots of issue related to memory. I am planning to learn to program in C/C++. I have beginner knowledge of C/C++ and I want to see some short sample why C/C++ can have issues with memory management. Please Provide some samples.

Edit
Report

2 Answers

7

Basically, in these languages, you have to manually request every bit of memory that is not a local variable known at compile time, and you have to manually release it when you don't need it anymore. The are libraries (so-called smart pointers) that can automate this process to some degree, but they are not applicable everywhere. Furthermore, there are absolutely no limits to how you can (try to) access memory via pointer arithmetics.

Manual memory management can lead to a number of bugs:

  • If you forget to release some memory, you have a memory leak
  • If you use more memory than you've requested for a given pointer, you have a buffer overrun.
  • If you release memory and keep using a "dangling pointer" to it, you have undefined behaviour (usually the program crashes)
  • If you miscalculate your pointer arithmetics, you have a crash, or corrupted data

And many of these problems are very hard to diagnose and debug.

answered 2010-06-05T06:31:39.583
5

I can honestly say that I have no "issues" with memory allocation when programming in C++. The last time I had a memory leak was over 10 years ago, and was due to blind stupidity on my part. If you write code using RAII, standard library containers and a small modicum of common sense, the problem really does not exist.

answered 2010-06-05T08:14:35.253

Your Answer