簡體   English   中英

基於指針的代碼在CodeBlocks中給出分段錯誤

[英]Pointers based code giving Segmentation fault in CodeBlocks

這是我的代碼。 我在代碼末尾不斷出現分段錯誤。 一切運行正常,但分段錯誤最終出現。

我正在嘗試准備跳棋游戲,第一步是用符號初始化每個棋子。 這段代碼試圖做同樣的事情。

代碼塊版本:13.12 Windows 7

#include <stdio.h>
#include <stdlib.h>

#define boardheight 8
#define boardwidth 8
struct Player_symbols{
    char symbol;
    char king_symbol;
};

struct Checker_piece{
    char king;
    int on_board;
    int num_moves;
    int player;
    struct Player_symbols* symbols;
};

int pieces_count[2] = {12,12};
struct Checker_piece* player_1_pieces;
struct Checker_piece* player_2_pieces;

void initialize_player_pieces(struct Checker_piece* player_pieces, struct Player_symbols* player_symbols, int player_num);
void initialize_board(struct Checker_piece* checker_board[boardheight][boardwidth],struct Checker_piece* player1,struct Checker_piece* player2);
void print_board(struct Checker_piece* checker_board[boardheight][boardwidth]);
int move_piece(struct Checker_piece* checker_board[boardheight][boardwidth], int x,int y,int player);
void continue_jumping(struct Checker_piece* checker_board[boardheight][boardwidth],int* y,int* x,int player);
int generate_destination(int x, int *dest_x, int *dest_y, int *dest_y_jump,int dest_y2,int dest_y2_jump,int move_flags,int player);

int main()
{

    struct Checker_piece* checker_board[boardheight][boardwidth];
    struct Checker_piece* dummy;
    struct Player_symbols* dummy_symbol;
    //declare and initialize the Checker_piece structures by allocating memory dynamically using a dummy structure to determine its size
    player_1_pieces = malloc(12*sizeof dummy);
    player_2_pieces = malloc(12*sizeof dummy);
    struct Player_symbols *player_1_symbols,*player_2_symbols;

    player_1_symbols = malloc(sizeof dummy_symbol);
    player_2_symbols = malloc(sizeof dummy_symbol);

    //initialize the player symbols
    player_1_symbols->symbol = 'o';
    player_1_symbols->king_symbol = 'O';
    player_2_symbols->symbol = 'x';
    player_2_symbols->king_symbol = 'X';

    initialize_player_pieces(player_1_pieces,player_1_symbols,1);
    initialize_player_pieces(player_2_pieces,player_2_symbols,2);

    printf("Done");
    return 0;
}

void initialize_player_pieces(struct Checker_piece* player_pieces, struct Player_symbols* player_symbols, int player_num){
    int i;
    for (i=0; i<12; i++, player_pieces++ ) {
        player_pieces->king='N';
        player_pieces->num_moves=0;
        player_pieces->on_board=1;
        player_pieces->player=player_num;
        player_pieces->symbols= player_symbols;
    }
}

check_board 應該struct數組時,它被聲明為指針數組。 同樣對於dummydummy_symboldummy_symbol ,您甚至不需要)。

如果你想malloc一個符號,那么malloc一個符號,而不是指向一個符號。 但是您甚至不需要malloc這些:只需聲明基於堆棧的變量:

struct Player_symbols player_1_symbols; // no pointer, no malloc needed

我建議您在代碼中稍作更改。 使用結構時,請動態地將內存分配給結構,而不是指向變量的指針。

struct Checker_piece* checker_board[boardheight][boardwidth];



 struct Checker_piece* dummy;
    struct Player_symbols* dummy_symbol;

    player_1_pieces = (Checker_piece *)malloc(12*sizeof(Checker_piece));
    player_2_pieces = (Checker_piece *)malloc(12*sizeof(Checker_piece));
    struct Player_symbols *player_1_symbols,*player_2_symbols;

    player_1_symbols = (Player_symbols *)malloc(sizeof(Player_symbols));
    player_2_symbols = (Player_symbols *)malloc(sizeof(Player_symbols));

PS-如果您在C語言中是programmimg,則無需鍵入強制轉換malloc。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM