-1

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,

helb
  • 7,609
  • 8
  • 36
  • 58
  • What's `i` at the segfault? – Bart Friederichs Nov 17 '14 at 08:28
  • it is 0.It occurs at the very beginning of the iteration. – user2185374 Nov 17 '14 at 08:36
  • 2
    I suspect that your `Equipment` instance is broken. How are you creating and using it? – molbdnilo Nov 17 '14 at 09:13
  • I suspect you are allocating your `Equipment` instance as a local variable (perhaps an array ?), and this is resulting in a stack overflow. Please include the relevant part of the code. – Paul R Nov 17 '14 at 09:26
  • What is the value of the `this` pointer at the line which throws the exception? – helb Nov 17 '14 at 12:18
  • Hi Paul, yes this instance is local.If I avoid creating this by straightway calling _smu[0] of _smuArray[i] cant solve the problem: Say, if(_smuArray[i]._smu[0] == L'\0' ) instead of EquipmentSMU _equipmentSMU=_smuArray[i];//segmentation fault coming at here wchar_t _tempSmu = _equipmentSMU._smu[0]; if(_tempSmu == L'\0' ) – user2185374 Nov 18 '14 at 10:14
  • Interesting thing is if I print _smuArray[i]._smu[0] value by print _smuArray[i]._smu[0] it gives me {'Q','X',0} which is correct. – user2185374 Nov 18 '14 at 10:17

1 Answers1

0

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.

tim
  • 1