RE: Custom error message for stack overflow in C++
August 19, 2021 at 1:16 pm
(This post was last modified: August 19, 2021 at 1:42 pm by HappySkeptic.)
The pass-by-reference changes look good.
Changing some data to static in TreeNode does solve some memory-use problems, but you have another potential problem. You are still initializing the data in the constructor of TreeNode, every time. This is wasteful, and rather nasty looking.
If you use static data, you need to find a way of initializing it only once. A non-thread-safe way would be to have another static int initializedStatics; Set it to false initially, and then your constructors can have an
if (! initializedStatics) {
... do initialization work
initializedStatics = true;
}
Note that this is a bad pattern, as it isn't thread-safe. It would be better to batch all the statics into a singleton, and initialize the singleton using some sort of thread lock (these are different in Windows and Linux, so it is a pain to program them cross-platform). However, your program isn't multi-threaded anyway.
I mentioned earlier about the separate .h (for class declarations) and .cpp files (for class method implementations and static variable implementation).
What you have done in TreeNode.cpp, with your static changes will work, but ONLY because everything is compiled into one link module (i.e. one program, not multiple libraries that bring in the TreeNode.cpp file). If multiple libraries were to import TreeNode.cpp (you should import .h or .hpp files, not the .cpp), there would be multiple implementations of the static variables, causing either a program link error (or if it links, could blow-up at runtime if not all versions are initialized). It may not affect your program, but it does look bad to a prospective employer if they were to see this code.
As for getting Windows to work, go into Visual Studio, and increase the reserve heap size in the link parameters. Sure, this is a pain if you are trying to build from CMake, as you would have to insert the parameter /HEAP:8388608,4096 to make an 8MB heap, for instance. It is possible to add parameters in CMake only for MSVC, but I'm not an expert. In Visual Studio, changing the Reserve Heap size in the link is just something in the GUI. Just make it 8388608 . If it still doesn't work, break on any runtime exceptions, and look at your program stack to find if you have an endless loop.
Changing some data to static in TreeNode does solve some memory-use problems, but you have another potential problem. You are still initializing the data in the constructor of TreeNode, every time. This is wasteful, and rather nasty looking.
If you use static data, you need to find a way of initializing it only once. A non-thread-safe way would be to have another static int initializedStatics; Set it to false initially, and then your constructors can have an
if (! initializedStatics) {
... do initialization work
initializedStatics = true;
}
Note that this is a bad pattern, as it isn't thread-safe. It would be better to batch all the statics into a singleton, and initialize the singleton using some sort of thread lock (these are different in Windows and Linux, so it is a pain to program them cross-platform). However, your program isn't multi-threaded anyway.
I mentioned earlier about the separate .h (for class declarations) and .cpp files (for class method implementations and static variable implementation).
What you have done in TreeNode.cpp, with your static changes will work, but ONLY because everything is compiled into one link module (i.e. one program, not multiple libraries that bring in the TreeNode.cpp file). If multiple libraries were to import TreeNode.cpp (you should import .h or .hpp files, not the .cpp), there would be multiple implementations of the static variables, causing either a program link error (or if it links, could blow-up at runtime if not all versions are initialized). It may not affect your program, but it does look bad to a prospective employer if they were to see this code.
As for getting Windows to work, go into Visual Studio, and increase the reserve heap size in the link parameters. Sure, this is a pain if you are trying to build from CMake, as you would have to insert the parameter /HEAP:8388608,4096 to make an 8MB heap, for instance. It is possible to add parameters in CMake only for MSVC, but I'm not an expert. In Visual Studio, changing the Reserve Heap size in the link is just something in the GUI. Just make it 8388608 . If it still doesn't work, break on any runtime exceptions, and look at your program stack to find if you have an endless loop.