0

I have a c++ application that runs a memory-consuming algorithm.

It crushes with error : terminate called after throwing an instance of 'std::bad_alloc' after the allocated memory reaches about 2GB.

I want to compile it as 64Bit so it could allocate more memory from my 64Bit/8GB Ram machine.

How it can be done?

Im working with CLion, building with CMake and using mingw version: mingw-w64\i686-4.8.3-posix-dwarf-rt_v3-rev2 btw - the wingw is installed under C:\Program Files (x86)\mingw-w64 is this related to the issue?

I've been looking here: Detecting 64bit compile in C and (not suprisingly) I can see that my application does not have the __x86_64__

Community
  • 1
  • 1
yossico
  • 3,421
  • 5
  • 41
  • 76
  • 1
    Doesn't -m64 do the trick? – SergeyA Nov 25 '15 at 18:13
  • mmm i dont know where should i put it? – yossico Nov 25 '15 at 18:15
  • As an argument to your compiler invocation string. Somewhere in CMake definitions. – SergeyA Nov 25 '15 at 18:16
  • ill check it in about an hour and tell you – yossico Nov 25 '15 at 18:17
  • If you can compile and run your application on some POSIX system, try using [valgrind](http://valgrind.org/). You might have some [memory leak](https://en.wikipedia.org/wiki/Memory_leak).If possible, compile it with [GCC 5](http://gcc.gnu.org/gcc-5/) and use some `-fsanitize=` options during the debugging phase (in addition of `-Wall -Wextra -g`) – Basile Starynkevitch Nov 25 '15 at 18:47
  • @SergeyA you where right, it worked! '-m64' did the trick, now I discovered that I need to change the toolchain from i686 to x86_64. Thanks! – yossico Nov 25 '15 at 21:42
  • @BasileStarynkevitch thanks for your advice, I'll check that. I kinda expected the huge memory consumption since Im working on an algorithm that builds HUGE graph with bilions of nodes – yossico Nov 25 '15 at 21:45
  • @SergeyA do you want to post your answer so I will accept it? – yossico Dec 03 '15 at 00:34

2 Answers2

4

You have to tell CLion to use MinGW-w64 to build your CMake project.

You dont have to change your CMake project configuration. It would be counterproductive limiting your CMake project by a fixed machine architecture, if you can instead easily tell your compiler which architecture to build for or use an appropriate compiler (MinGW-w64).

This post shows you how to do that:

https://dev.my-gate.net/2014/09/15/how-to-use-mingw-w64-with-clion/

Excerpt from this post:

Here are the few steps to make it work:

  1. Get your MinGW-w64 compiler installed somewhere.
  2. Create a file with the path [MinGW-w64 dir]/include/_mingw.h and the content shown below.
  3. Point CLion to your [MinGW-w64 dir] and watch how CLion recognizes your MinGW setup. Image screenshoted from this post

After building your executable, you can check its architecture with the unix command file, which should be available to you since you are using mingw.

MarkusAtCvlabDotDe
  • 1,032
  • 5
  • 12
3

As suggested by OP, posting my comment as an answer: for this you need to pass -m64 option to compiler/linker.

SergeyA
  • 61,605
  • 5
  • 78
  • 137