Hi guys....i have a problem and I hope you can explain me where am I wrong...

I am doing some kind of Breadth First Search (BFS) ("some kind of" because this is my first program to use this algorithm)

I have an array of size 4^9 (262144 for those of you who dont have nerves to calculate or dont want to) and i have a pointer to access that array...
Problem occurs when i get close to the end of the array...something happens and then Segmentation Fault pops out...
i am not quite sure what is the problem. it could be indexing problem but i dont use array in standard manner like: array[index] anywhere in code but i use pointer to write data to it, and pointer is constantly being moved towards the end of the array....
I thought it could be stepping over the edge of the array but when i use debug output, i see that last position of that pointer is 261886 which is far from the end...(well....far enough)
Pointer is declared as struct situations *read; array: struct situations sits[262144]; and then: read = sits; (could be read = &sits[0]; but didnt want to complicate)
what could be reason for Segmentation Fault if it isn't:
1. dereferencing NULL pointer
2. dereferencing uninitialized pointer
3. accessing memory out of bounds or array indexing out of bounds

According to wikipedia:
A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed.
Must read article:
http://en.wikipedia.org/wiki/Segmentation_fault

ok....but i dont get it....i have a debug print to tell me how far am i from the end of the array and Segmentation Fault happens to occur some 1000 array elements before the end of the array...(and i am not doing any calculations like a[i+100] = ...

ok...i think i've found the problem...
problem occured at nearly the same place even if i doubled or tripled the size of the array...
it seems that recursion is the main problem because with some input data, function recalls itself even up to 27 times causing stack overflow (i guess)

here's the code so you dont make the same mistake and make sure you read wiki as adatapost said

int rotateClocks (void)
{
    // some declarations here
    for (i = 0; i < 9; i++)
    {
       // come calculations here
       if (something) return 1;
    }
    if (rotateClocks())
        return 1;

    return 0;
}

this way exit from function is ensured, but it happens not to not before 27. level of recursion (with some input data) so i had to change things a bit:

int rotateClocks (void)
{
    // some declarations here
    for (i = 0; i < 9; i++)
    {
       // come calculations here
       if (something) return 1;
    }
   return 2; // Signal to main to call the function again. It is used so the function will not call itself too many times and cause a segmetation fault

    return 0;
}

and dont forget how to store data you calculate (you cannot pass variables to child function as a parameter because you call child function from main), because in second example function completely finishes it work and the same one is called again from main
i used global variables where i needed :D
actually you could return variable instead returning number 2 as a signal and you could use that returned value to pass it to child function...but that is other story

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.