C++ Segmentation fault in AIX

492 views Asked by At

Hi I have written a piece of code in C++ at AIX 6 as part of my project as below: It compiles and build properly.But getting segmentation fault on executing below lines, "EquipmentSMU _equipmentSMU=_smuArray[i];"

I am building it with bjam and linking all libraries with -bmaxdata:0x80000000 option. I have tried it with export $LDR_CNTRL=MAXDATA=0x80000000 before executing it but unable to solve the problem.

I have ran the same code in windows, there it runs well without having any issues.

My Code :

#define EQUIP_MAX_SMU_LEN 30
#define EQUIP_MAX_SMU 100
typedef struct
{
    wchar_t _smu[EQUIP_MAX_SMU_LEN+1];

} EquipmentSMU;

class Equipment
{
public:
    Equipment();
    ~Equipment();

private:
    void _AddSMU(wchar_t* smu);

private:
    EquipmentSMU _smuArray[EQUIP_MAX_SMU];
};

void Equipment::_AddSMU(wchar_t* smu)
{
    int i;
    for ( i=0; i < EQUIP_MAX_SMU; i++ )
    {
        EquipmentSMU _equipmentSMU=_smuArray[i];//segmentation fault coming at here
        wchar_t _tempSmu = _equipmentSMU._smu[0];
        if(_tempSmu == L'\0' )
        {
            wcsncpy( _smuArray[i]._smu, smu, EQUIP_MAX_SMU_LEN+1 );
            return;
        }
        if( wcsncmp( _smuArray[i]._smu, smu, EQUIP_MAX_SMU_LEN+1) == 0 )
        {
            return;
        }
    }
}

Can anyone please help me to solve this problem. Thanks,

1

There are 1 answers

0
tim On

You are putting it on the stack instead of the heap and AIX is not a fan of this.

Declare a pointer *_smuArray and use it in your construtor as _smuArray = new type[i] which should allcoate it to the heap. Don't forget to put delete[] _smuArray and _smuArray = NULL in your destructor. I spent 3 hours last night finding this out :)

You will probably find that your code works fine as it is if you put it in main(). It is one of those goofy AIX things.