I have C implementation of an algorithm that includes shared library. The performance of the shared library is considerably different in C and C++ (performing much better in C++). Therefore, I have changed the shared library parts of the C implementation (Imp-1) into C++ (Imp-2) while keeping the rest of it same. I have compiled Imp-1 and Imp-2 by using gcc and g++ in Linux, respectively. In small sized problems both Imp-1 and Imp-2 perform exactly same. However, in a same large sized problem while Imp-1 solves it without any problem, Imp-2 returns std::bad_alloc error. Interestingly, this error occurs when the memory usage increases to 4GB where the available memory is 35GB.
Why this error occurs? (Compiler, operating system, compiler option, etc.)
Best Regards.
It seems that your gcc is a 64 bit compiler where as g++ is a 32 bit compiler. Even if you are running on a 32 bit system, unless you compile your program with a 64 bit compiler, your program cannot fully leverage the 64 bit addressing capabilities which would result a bad_alloc as you are experiencing if you want to address more than 4GB.
The reason I am confident that you are running a 32 bit compiled program on a 64 bit is the 4GB limit. Generally speaking, the memory is split as User/Kernal Space and on a 32 bit system the entire 4GB is not available, usually it is between 2GB and 3GB. As you can address the entire 4GB it seems you are running a 32 bit program on a 64 bit environment.