1

I am trying to have a function take some integer pointers passed from my main() function and assign values to them. However, my program crashes when assigning values. Here is my code:

int computeMoveLocation(int* r, int* c, char* board)
{
    //some code up here
    *r = 0;    //This breaks the program
    *c = 0;
}

I'm not trying to change the address of the pointer--I'm trying to change the value of the integer being pointed to. However, I am apparently doing something wrong.

Any help would be greatly appreciated.

EDIT: Here is the relevant code from main(). Please let me know if I should include anything else as well.

int main()
{
    //initialization code
    //...

    while (1)
    {

        switch (MACHINE_STATE)
        {
            case COMPUTER_MOVE :
            {
               //check rows for three Xs
               //check columns for three Xs
               //check diagonals for three Xs
               //otherwise, move anywhere else
               int *r, *c;
               computeMoveLocation(r, c, board);
               computerMove(*r,*c, board);
               PREVIOUS_STATE = COMPUTER_MOVE;
               MACHINE_STATE = HUMAN_MOVE;
               break;
            }

            //Other cases
        }//end switch
    }//end while
}//end main
cvenomz
  • 43
  • 1
  • 6
  • 4
    Show the main function and how you are passing in the pointers. – Barış Uşaklı Mar 03 '13 at 04:00
  • Please show us how you call computeMoveLocation? What is passed as r, c and board? – Jay Mar 03 '13 at 04:00
  • 1
    Did you allocate memory that `r`, `c`, and `board` point to? – JohnnyHK Mar 03 '13 at 04:02
  • 1
    Welcome to Stack Overflow. Please read the [FAQ] before too long. For a question like this, the problem is not in the lines you show; it is either in the 'some code up here' section of the code, or in the code that calls this function (or both). You'll need to provide an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) so that we can see what's causing the trouble, because the trouble is not in the code you do show. – Jonathan Leffler Mar 03 '13 at 04:07
  • Nothing seems wrong with the code above , are you passing proper adrress to the function , such as compute(&var,&var1,&var2) and not compute(var,var1,var2) – Barath Ravikumar Mar 03 '13 at 04:08
  • Thanks guys for all your help so far. I added the relevant code from main() where this function is called. Hopefully this will be able to show where I went wrong. – cvenomz Mar 03 '13 at 04:13
  • As one the comments say above, nothing is wrong with the code shown; however, the caller of computeMoveLocation does not insure that the pointers are not null or pointing incorrectly. Looi at second answer for a tutorial like answer. – mozillanerd Mar 03 '13 at 04:19

3 Answers3

10

You are passing in pointers but you didn't allocate memory. So they are pointing at a random location in memory.

int computeMoveLocation(int* r, int* c, char* board) {
    //some code up here
    *r = 0;    //This breaks the program
    *c = 0;
}

Bad main :

int main() {
    int *r;
    int *c;
    char *board;
    // bad, passing in pointers but didn't allocate memory
    computeMoveLocation(r, c, board); 
    return 0;
}

Good main #1:

int main() {
    int r = 5;
    int c = 5;
    char board = 'a';
    // fine, passing address of variables on stack
    computeMoveLocation(&r, &c, &board); 
    return 0;
}

Good main #2:

int main() {
    int *r = malloc(sizeof(int));
    *r = 5;
    int *c = malloc(sizeof(int));
    *c = 5;
    char *board = malloc(sizeof(char));
    *board = 'a';
    // fine, passing pointers that point to heap
    computeMoveLocation(r, c, board); 

    free(r);
    free(c)
    free(board);
    return 0;
}
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
1

You can always pass a pointer and modify the value that the pointer is pointing to. That is how pointers are supposed to be used. But, you should also be careful to see if the pointer does really points to something or not. The pointer should contain a valid address, the value at whose location you can change. If you don't ensure that, then Undefined Behavior will result.

For Example, when you call your computeMoveLocation function, the address which you are passing should either be of stack or of heap. You can see the code below to understand it.

First possibility

int r, c;
char board;
computeMoveLocation(&r,&c, &board);

Second Possibility

int *r, *c;
char *board;

r = malloc(sizeof(int));
c = malloc(sizeof(int));
board = malloc(sizeof(char));
computeMoveLocation(r,c,board);

Please note that char * is also generally used to pass an address to array of charaters, but, in a such a usage, generally it is ensure that it is null terminated or an accompanying length of the array is also passed.

You can anyway get more details on passing aroung pointers by a simple google search.


EDIT Now, that you have posted your code which calls computeMoveLocation, you see that you should modify your code according to the Second Possibility shown above, since you are declaring r and c as pointers or you should declare them as integers and call as per the First Possibility shown above. But, you are not doing the same which causes the undefined behavior.

Also, in the above examples, I have allocated memory for board, but, in your case, if it originates at some other place and if it has been handled appropriately there, then it need not be malloced.

Jay
  • 24,173
  • 25
  • 93
  • 141
  • `board` doesn't have to be `malloc`d since it originates elsewhere (probably an array defining the chessboard). – nneonneo Mar 03 '13 at 04:17
  • I had started my answer before he posted his code which calls computeMoveLocation. I edited my answer to reflect your thoughts. But, in my opinion, he should also check if board is being properly created wherever it originates. – Jay Mar 03 '13 at 04:24
1
int *r, *c;
computeMoveLocation(r, c, board);
computerMove(*r,*c, board);

You define a pointer but don't make it point to anything. Thus, it is a wild or uninitialized pointer; accessing *r as you do in computeMoveLocation will cause undefined behaviour (in your case, a crash).

You have to either initialize the pointer to point at something known, or just pass the address of an existing int:

int r, c;
computeMoveLocation(&r, &c, ...);

or

static int x, y; // static: only one instance of the variable exists
int *r = &x; // initialize pointers
int *c = &y;
computeMoveLocation(r, c, ...);

or

int *r = malloc(sizeof(int));
int *c = malloc(sizeof(int));
computeMoveLocation(r, c, ...);

In that last case, make sure to free the memory afterwards.

nneonneo
  • 171,345
  • 36
  • 312
  • 383