0

I have what I consider a really strange problem. I have a function with the following prototype:

void generateNodes(const int maxX, const int maxY, node nodes[]);

As one of the first things in this function I define a 2d array of shorts, which i use as boolean values. But when I call this function the value of maxY changes to a large value. The code in question is below:

void generateNodes(const int maxX, const int maxY, node nodes[]){
    int i, currentX, currentY;

    short used[MAX_NODES][MAX_NODES];

    //Generate the nodes
    for(i = 0; i < MAX_NODES; i++){
        currentX = randomNumber(0,maxX);
        currentY = randomNumber(0,maxY);

        nodes[i].color = 0;
        nodes[i].numberOfConnections = 0;
        nodes[i].id = i;
        nodes[i].distanceFromStart = NOT_SET;
        nodes[i].parent = NULL;

        if(!used[currentX][currentY]){
            nodes[i].x = currentX;
            nodes[i].y = currentY;

            used[currentX][currentY] = 1;
        } else {
            i--;
        }
    }

    int numberOfConnections, j, currentNeighbor;

    //Generate the connections
    for(i = 0; i < MAX_NODES; i++){
        numberOfConnections = randomNumber(1,5); //Between one and five outgoing connections

        for(j = 0; j < numberOfConnections; j++){

            currentNeighbor = randomNumber(0,19); //Select the neighbor

            while(currentNeighbor == i){
                currentNeighbor = randomNumber(0,19); //Try again while the selected is self
            }

            nodes[i].canReach[++(nodes[i].numberOfConnections)] = &nodes[currentNeighbor];

            nodes[currentNeighbor].canReach[++(nodes[currentNeighbor].numberOfConnections)] = &nodes[i];
        }
    } 
}

MAX_NODES is defined to 20.

Does anyone know why this might happen?

10
  • 2
    How do you know the value for maxY changes? Do you print it, or do you see it change in a debugger? Commented Nov 28, 2012 at 15:37
  • Also, when exactly does the change occur? Commented Nov 28, 2012 at 15:37
  • I use the debugger in Eclipse, and the change occurs before the next line after short used[MAX_NODES][MAX_NODES]; Commented Nov 28, 2012 at 15:41
  • What platform are you running this on? Is it possible that 20 * 20 * sizeof (short) is approaching the stack size? Commented Nov 28, 2012 at 15:44
  • I am on mac os x mountain lion. I tried using malloc instead, but the problem persisted. Commented Nov 28, 2012 at 15:46

2 Answers 2

2

Very probably the code in ... is accessing beyond the end of used, causing arguments to be smashed. Without the code, it's of course impossible to say.

Sign up to request clarification or add additional context in comments.

2 Comments

@AshRj No, I was trying to express that the code might be accessing out of bounds. I re-wrote to clarify.
I have now added the rest of the function, do you see something bad happening?
1

Since you do not seem to initialize the array used, it may well be that some elements are considered used (!= 0), since an array on stack is not initialized to zero, but takes whatever was in that memory area before. An if an X,Y pair is considered used, you decrement the loop counter, possibly beyond zero into the negative realm, possibly overwriting - on the next iteration - part of the stack. This may also change the parameters, since they also reside on the same stack, before the local array.

Start with initializing used, and consider rewriting the loop to not change the loop variable except in the for statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.