使用2d数组写入访问冲突
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用2d数组写入访问冲突相关的知识,希望对你有一定的参考价值。
我正在尝试将字符放入我的2D数组中。我已经定义了这些对象:
typedef struct board {
char* board[25][80];
}Board;
typedef struct obstacleA {
int* x;
int* y;
}ObstacleA;
typedef struct obstacleC {
int* x;
int* y;
}ObstacleC;
typedef struct obstacleB {
int* x;
int* y;
}ObstacleB;
typedef struct Star {
int *x;
int *y;
int *power;
}Star;
typedef struct Cross {
int *x;
int *y;
int *power;
}Cross;
我的spawn函数为我的对象提供随机数(或坐标)
void spawn(Board* board, Cross* cross, Star* star, ObstacleA* A, ObstacleB* B, ObstacleC* C) {
srand(time(NULL));
cross->x = (1 + rand() % 24);
cross->y = (1 + rand() % 79);
star->x = (1 + rand() % 24);
star->y = (1 + rand() % 79);
A->x = (1 + rand() % 24);
A->y = (1 + rand() % 79);
B->x = (1+ rand() % 24);
B->y = (1+ rand() % 79);
C->x = (1 + rand() % 24);
C->y = (1 + rand() % 79);
putBoard(&board, &cross, &star, &A, &B, &C);
}
并且putBoard函数将字符放在适当的坐标中:
void putBoard(Board* board, Cross* cross, Star* star, ObstacleA* A, ObstacleB* B, ObstacleC* C) {
board->board[*star->x][*star->y] = '*';
board->board[*cross->x][*cross->y] = '+';
board->board[*A->x][*A->y] = 'A';
board->board[*B->x][*B->y] = 'B';
board->board[*C->x][*C->y] = 'C';
}
但是,在运行程序时,我得到一个“异常抛出:写入访问冲突。板是0x21C3BD2。”在“board->board[*C->x][*C->y] = 'C';
”行。
答案
当你调用putBoard
时,你会将一个指针传递给指向该板的指针。也就是说,你传递了Board **
类型的东西。与传递给putBoard
的其他参数相同,您传递指针指针。
从&
调用putBoard
时,不要使用address-of运算符spawn
。
一个好的编译器应该警告你传递不兼容的指针类型。
正如评论所说的那样,你已经超越了指针。在大多数使用指针的地方,你根本不需要它。
另一答案
您正在为地址分配随机值,这不是您想要的:
cross->x = (1 + rand() % 24);
cross->y = (1 + rand() % 79);
star->x = (1 + rand() % 24);
star->y = (1 + rand() % 79);
A->x = (1 + rand() % 24);
A->y = (1 + rand() % 79);
B->x = (1+ rand() % 24);
B->y = (1+ rand() % 79);
C->x = (1 + rand() % 24);
C->y = (1 + rand() % 79);
由于你在x
,y
指针中有一个随机值,你在程序崩溃的行中有两个问题:
board->board[*C->x][*C->y] = 'C';".
1)取消引用随机地址通常会使程序崩溃。
2)即使你的程序在该操作中存活了derefernced值
*C->x, *C->y
可以很容易地超出board
阵列的边界。这也可能导致程序崩溃。
你肯定过度使用结构中的指针。你不需要它们。您应该注意的是将指针传递给您的结构,以便可以修改内部成员。
3)程序应尽可能简单,过度使用指针会使其更难理解并容易出错。
putBoard(&board, &cross, &star, &A, &B, &C);
应该:
putBoard(board, cross, star, A, B, C);
如上所述,编译器应该警告您传递不兼容的指针类型。
你的程序可以用你的结构修复,但更简单总是更好。看一眼:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROWS 6
#define COLUMNS 50
typedef struct board {
char board[ROWS][COLUMNS];
}Board;
typedef struct obstacleA {
int x;
int y;
}ObstacleA;
typedef struct obstacleC {
int x;
int y;
}ObstacleC;
typedef struct obstacleB {
int x;
int y;
}ObstacleB;
typedef struct Star {
int x;
int y;
int power;
}Star;
typedef struct Cross {
int x;
int y;
int power;
}Cross;
void putBoard(Board* board, Cross* cross, Star* star, ObstacleA* A, ObstacleB* B, ObstacleC* C) {
board->board[star->x][star->y] = '*';
board->board[cross->x][cross->y] = '+';
board->board[A->x][A->y] = 'A';
board->board[B->x][B->y] = 'B';
board->board[C->x][C->y] = 'C';
}
void spawn(Board* board, Cross* cross, Star* star, ObstacleA* A, ObstacleB* B, ObstacleC* C) {
srand(time(NULL));
cross->x = (1 + rand() % (ROWS-1));
cross->y = (1 + rand() % (COLUMNS-1));
star->x = (1 + rand() % (ROWS-1));
star->y = (1 + rand() % (COLUMNS-1));
A->x = (1 + rand() % (ROWS-1));
A->y = (1 + rand() % (COLUMNS-1));
B->x = (1+ rand() % (ROWS-1));
B->y = (1+ rand() % (COLUMNS-1));
C->x = (1 + rand() % (ROWS-1));
C->y = (1 + rand() % (COLUMNS-1));
putBoard(board, cross, star, A, B, C);
}
void printBoard(Board* board)
{
for(int i= 0; i< ROWS; i++){
for(int j= 0; j< COLUMNS; j++) {
printf("%c", board->board[i][j]);
}
printf("
");
}
}
void cleanBoard(Board* board)
{
for(int i= 0; i< ROWS; i++){
for(int j= 0; j< COLUMNS; j++) {
board->board[i][j] = ' ';
}
}
}
int main(void)
{
Board board;
ObstacleA A;
ObstacleC C;
ObstacleB B;
Star S;
Cross CR;
cleanBoard(&board);
spawn(&board, &CR, &S, &A, &B, &C);
putBoard(&board, &CR, &S, &A, &B, &C);
printBoard(&board);
return 0;
}
输出:
C
*
A
+
B
以上是关于使用2d数组写入访问冲突的主要内容,如果未能解决你的问题,请参考以下文章