Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Segmentation fault at declaration IloEnv env;

    Posted 06/23/16 06:50 PM

    Originally posted by: AlexanderVi


    So, I came across rather weird error. I have CPELX 12.6 running in Ubuntu (I had the same issue earlier in Linux Mint). I get error "Segmentation fault (core dumped)" in the following code:

    #include <ilcplex/ilocplex.h>
    ILOSTLBEGIN
    int times;
    int main()
    {
            cout << "start\n";
            IloEnv env;
            cout << "finish\n";
            return 0;
    }
    

    Turns out that the error goes away if I rename the variable "times" to anything else, or make it local instead of global. I, of cause, don't have to use a global variable called "times", but was still wondering if anyone can explain what is going on, or if there are other names I should avoid. My .cpp and a Makefile attached.

     

    Thanks, 
    Alex.

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Segmentation fault at declaration IloEnv env;

    Posted 06/24/16 03:24 AM

    I think the problem is that you have a global variable named times while there also a a global (C) function called times() and the IloEnv constructor invokes this function. I can reproduce the crash here and in the debugger I see this:

     

    Program received signal SIGSEGV, Segmentation fault.
    0x00000000007fa884 in times ()
    (gdb) bt
    #0  0x00000000007fa884 in times ()
    #1  0x000000000040bf43 in IloTimer::getCPUtime() ()
    #2  0x000000000040c008 in IloTimer::start() ()
    #3  0x0000000000418856 in IloEnvI::IloEnvI() ()
    #4  0x0000000000418f45 in IloEnv::IloEnv() ()
    #5  0x00000000004061f7 in main () at ./Main.cpp:11
    (gdb) f 5
    #5  0x00000000004061f7 in main () at ./Main.cpp:11
    11        IloEnv env;
    (gdb) p times
    $1 = 0
    (gdb) p &times
    $2 = (int *) 0x7fa884 <times>

    As you can see, the linker set up the code so that instead of calling the global function times(), your variable times is called as if it was a function. That is for sure asking for trouble :-)

    Not sure if you can fix this using a different link order or using special linker arguments. In any case, I think it is bad practice to have global variables in C++. If I put the 'times' variable in an (potentially anonymous) namespace then things work as expected.


    #CPLEXOptimizers
    #DecisionOptimization