Public Group

# segmentation fault (access violation)

This topic is 4756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

************************************ DISCLAIMER: I KNOW THIS CODE IS UGLY ************************************ ok... now that I took care of that, I need help identifying how to fix a segmentation fault. It occurs on this line in my code: iPossibilities[possibilities][COUNTFINAL] = iWord[POS]; // <-- THIS ONE Maybe I am having troubles figuring this out because I have written some code that should probably never be seen it is so terrible. I have 6 for loops nested... lol... but I am just doing this to get an answer for an extra credit problem in math, and I only have tonight. Anyways, here is my function abomination: (sorry I'm not sure what code tags are)

/***************************************************************************
*******void iGenerateList(int iWord[5], int iPossibilities[121][5])
***************************************************************************/
void iGenerateList(int iWord[5], int iPossibilities[121][5])
{
int i,j,k,l,m;
int possibilities = 0;
// int temp[5];
int used[6]; //levels of usage

//BEGIN THE GENERATION!!!
/*
* A note to all -
*     THIS IS THE MOTHER OF ALL NESTED FOR-SETS!!!
*/
for(i = 0; i < 5; i++) //for
{
used[0] = i;
for(j = 0; j < 5; j++) //for
{
if(j == used[0])
{ // nothing
}
else
{ used[1] = j; }

for(k = 0; k < 5; k++) //for
{
if((k == used[0]) || (k == used[1]))
{ // nothing
}
else
{ used[2] = k; }

for(l = 0; l < 5; l++) //for
{
if((l == used[0]) || (l == used[1]) || (l == used[2]))
{ // nothing
}
else
{ used[3] = l; }

for(m = 0; m < 5; m++) //for
{
if((m == used[0]) || (m == used[1]) || (m == used[2]) || (m == used[3]))
{ // nothing
}
else
{ used[4] = m; }

possibilities++;
int COUNTFINAL = 0;
for(COUNTFINAL = 0; COUNTFINAL < 5; COUNTFINAL++) //for
{
int POS = used[COUNTFINAL];
iPossibilities[possibilities][COUNTFINAL] = iWord[POS]; // <-- HERE IT IS ******************************* THE BAD LINE!!!
}//for
}//for
}//for
}//for
}//for
}//for

//omg its over

}//function


If somebody can figure out what I am doing wrong, I would be very happy. I don't think I'm writing past the end of the arrray, and it still occurred when I put limitations on possibilities ( less than 120 ) so i dunno... Please Help!

##### Share on other sites
Here's some cleaned up code. I'm not sure if it fixed anything but I'm sure later posters will be sure to appreciate it more.

void iGenerateList(int iWord[5], int iPossibilities[121][5]){    int i,j,k,l,m;    int possibilities = 0;    int used[6];    int COUNTFINAL;    for(i = 0; i < 5; i++)    {        used[0] = i;        for(j = 0; j < 5; j++)        {            if(j != used[0])            { used[1] = j; }                    for(k = 0; k < 5; k++)            {                if((k != used[0]) || (k != used[1]))                { used[2] = k; }                                for(l = 0; l < 5; l++)                {                    if((l != used[0]) || (l != used[1]) || (l != used[2]))                     { used[3] = l; }                                          for(m = 0; m < 5; m++)                     {                         if((m != used[0]) || (m != used[1]) || (m != used[2]) || (m != used[3]))                         { used[4] = m; }                                                  possibilities++;                         for(COUNTFINAL = 0; COUNTFINAL < 5; COUNTFINAL++)                         {                             iPossibilities[possibilities][COUNTFINAL] = used[COUNTFINAL];                         }                     }                }            }        }    }}

Try copying and pasting that into your compiler and see if it made any difference. I DID change some things in the structure and how things were done.

##### Share on other sites
possibilities is being incremented every time through the 'm' loop. The increment is being executed 5x5x5x5x5=3125 times. So, you are indexing well past the 121 slots allocated for that array dimension.

edited for clarification

##### Share on other sites
thanks for cleaning it up, it helps a lot since i had to just throw this together. unfortunately, the clean code still has a segmentation fault... on the same line...

##### Share on other sites
I see what you mean about that overindexing... but I put a limitation around the code at fault and

if (possibilities < 120)
{
}

I still have the fault...

##### Share on other sites
Quote:
 Original post by koooooI see what you mean about that overindexing... but I put a limitation around the code at fault andif (possibilities < 120){ then do bad line...}I still have the fault...

Then you might want to post more code. It may be that the array you are passing in to that function isn't really the size you think it is.

Also, you might be messing up the used array. If one of the elements isn't set correctly, you are going to get garbage when you attempt:
    used[POS]

##### Share on other sites
Okay, i'm not sure - i'm never very good with these things, but since you need quick help i'll post anyway. Just at a glance, you have 6 for loops which each will loop 5 times, then you increment 'possibilities' in the innermost loop. So that way, possibilities will be 5 to the power of 6 or something, way more than 121. So you acces an array out of bounds with that, leading to a segfault. Is that it?

Anyway, if you output the value of the variables you use as index, you might find out whether it's out of bounds, as it sure looks like it.

Damn that loop looks like something I did in my first game some months ago, thanks for reminding me i have made some progress!

Good luck with the credits.

##### Share on other sites
Hey i figured something out! I had to initialize all of the used[] variables to a value before I could write to them. Don't know for sure why, but I'm assuming it has to to with memory allocation. Of course the program doesn't find the values that I want, but I'll work on that. Thanks for the help guys.

EDIT: I moved possibilities into the conditional statement in the for loop with four (..) || (..) so that I wouldn't have way too many possibilities also

EDIT: Lol thx for the tip on used[] Dave. Sry im trying to move around too fast

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013393
×