PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 4, 2022

[FIXED] Why does my C program crash when assigning a value to an int pointer?

 July 04, 2022     c, pass-by-reference, pointers     No comments   

Issue

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

Solution

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;
}


Answered By - Barış Uşaklı
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,210,826

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing