Fatal signal 6 (SIGABRT) at … (Cocos2dx 3.x on Android with C++) + Eclipse

Posted on Updated on

While it makes sense to use if statements its also blimin’ messy once you have so many conditions and editing the code later becomes messy, which eventually leads to bugs and hours of headaches.

I had an array like so:

MyClass::Constructor()
: MyArray({"C++", "C#", "Objective-C", "C"})
{
    // コンストラクター
}

Then I had to loop through this and tried to optimize my code so that instead of having something like this:

if (strcmp(MyArray,"C++") == 0){
    CCLog("array has C++");
} 
else if (strcmp(MyArray,"C#") == 0){
    CCLog("array has C#");
} 
if (strcmp(MyArray,"Objective-C") == 0){
    CCLog("array has Objective-C");
} 
if (strcmp(MyArray,"C") == 0){
    CCLog("array has C");
} 

I could turn it into something like this:

for (int i = 0; i < sizeof(MyArray); i++) {
    switch(i) {
        case 0: CCLog ("array has C++"); break;
        case 1: CCLog ("array has C#"); break;
        case 2: CCLog ("array has Objective-C"); break;
        case 3: CCLog ("array has C"); break;
    }

But the problem occurs when I get this error message:
Fatal signal 6 (SIGABRT) at …

I tried to print all the values of my array and commented out the switch statement and it seems that my loop will go through the array, but then read it from start to finish again and output (null)

The solution
I ended up not using sizeof, but just changed the above code to this:

for (int i = 0; i < MyArray.size(); i++) {
    switch(i) {
        case 0: CCLog ("array has C++"); break;
        case 1: CCLog ("array has C#"); break;
        case 2: CCLog ("array has Objective-C"); break;
        case 3: CCLog ("array has C"); break;
    }

Then recompiled and finally I was able to get my loop to work properly without issue.

Not really sure what sizeof really means, but i had assumed it worked the same as .size() and guessed wrong.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s