Neural Network in c++

So I built a basic neural network in c++, but it was completely infested with memory related bugs, so I am starting the project again, hoping that I can learn from my mistakes. However, before I start anew, I want to see if there is any valuable advice that someone with experience can give me.
Also, I really need to understand the nature of the runtime errors that I've been getting:

'malloc(): memory corruption (fast):' and 'Segmentation Fault'
What should I be cautious to not do while programming to not get these errors?

Also is it a total taboo to do MALLOC_CHECK_=0 so runtime malloc errors don't stop my program?

Finally, what things other than dynamic memory do I have to explicitly free up (delete or delete[])?

Thanks for any help!
Last edited on
What should I be cautious to not do while programming to not get these errors?
Writing outside of array bounds. Reading outside of array bounds. Double deletes. Deleting non-newed pointers. Uninitialized variables (particularly deadly when combined with optimized switches).
Basically, do not try to get clever around memory management, because you'll just screw it up.

Also is it a total taboo to do MALLOC_CHECK_=0 so runtime malloc errors don't stop my program?
In all likelihood you'll just make your program much harder to debug. The memory bugs do not go away just because the program doesn't crash. The memory just silently gets progressively more corrupt until the program crashes and it becomes impossible to tell where the actual bad code is.
For example, if in some place you delete an invalid pointer and the program doesn't crash immediately, the program might crash at an arbitrarily distant future time during, say, a perfectly correct new, or even a call to std::map::operator[]() (as those may allocate memory).

Finally, what things other than dynamic memory do I have to explicitly free up (delete or delete[])?
None. A pointer must be deleted if and only if it has been allocated by new. The same goes for delete[] and new[].
But, really, you should avoid managing memory manually, if possible. Just use local objects or smart pointers.
Code like this:
1
2
3
T *p = new T;
//...
delete p;
must be avoided at all costs in exception-safe code.
Are you using the standard library? I am asking because you sound like a person who has experience working with complicated concepts but perhaps not with C++ (this was my situation when I came to this forum 7 years ago).


If I were you, I'd try to use std::vector if possible. I would also recommend mandatory index-out-of bound checking (slows your code by a tiny bit but saves so much headache it's worth the slower code). By index out of bound checking I mean something like:

1
2
3
4
5
6
7
8
9
10
11
12
template<typename T>
class myVector{
std::vector<T> theArray;
public:
T& operator[](unsigned int x)
{ if (x>=this->theArray.size())
  { //...display a nasty message indicating the value of faulty index and what is the allowed range
    assert(false);
  }
  return this->theArray[x];
}
};

If you want to make stand-alone pointers to stand-alone objects you definitely should look into helios' advice, more precisely read the description of:
std::unique_ptr and std::shared_ptr. Those two classes are simply wrappers for pointers with some cool automation [making them a tiny tiny bit slower] so you don't need to release the memory those pointers point to manually - for example, there's no danger of double-freeing memory (if you call delete on an already deleted pointer).
Last edited on
Yea i have been using std::vector for everything involving arrays in this project.
My biggest problem (I think) is that I wrote almost all of the code, and didn't run it until much later.

I'm going to start off from scratch. My code isn't worth the headache of debugging...

EDIT: looking over the code you posted, I definitely should have embedded std::vector into my own class, along with some functions that prevent indexing errors.
Last edited on
http://www.cplusplus.com/reference/vector/vector/at/

Or you may define the _GLIBCXX_DEBUG macro when compilling (gcc), which will make checks on iterators and operator[] in vector (but I think that it wouldn't with C-arrays)
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
Last edited on
Thanks I'll definitely look into those options!
Topic archived. No new replies allowed.