我尝试复制我在项目中遇到的这个错误,但所有的努力都失败了,我的复制完全编译和测试。
基本上,我不断收到一个seg错误,该错误追溯到我的一个文件中对strcpy
的调用。我已经成功地将其隔离了一些,并且我倾向于认为问题是由于之前调用了memcpy
,因为没有该行,代码工作正常。
void play(char * p1, char * p2,
struct result * final_res)
{
enum icons my_board[WIDTH][HEIGHT];
initialize(my_board); /* potentially causing the seg fault */
strcpy(final_res -> won, "Hello"); /* the seg fault */
strcpy(final_res -> lost, "Goodbye");
/* rest of my code, commented out */
}
result
中的一个结构:
struct result
{
char won[21];
char lost[21];
};
有问题的函数,它复制全局枚举的状态master
:
void intialize(enum icons board[][WIDTH])
{
int i, j;
for(i=0; i < WIDTH; i++) {
for(j=0; j < WIDTH; j++) {
memcpy(&board[i][j], &master[i][j], WIDTH-1);
}
}
}
master
全局变量:
static const enum icons master[WIDTH][HEIGHT] =
{
{CIRCLE, CIRCLE, CIRCLE, CIRCLE, CIRCLE},
{CIRCLE, CIRCLE, CIRCLE, CIRCLE, CIRCLE},
{SQUARE, SQUARE, SQUARE, SQUARE, SQUARE},
{TRIANGLE, TRIANGLE, TRIANGLE, TRIANGLE, TRIANGLE},
{TRIANGLE, TRIANGLE, TRIANGLE, TRIANGLE, TRIANGLE},
};
我的valgrind输出:
==11706== Invalid write of size 1
==11706== at 0x343040: _platform_memmove$VARIANT$Nehalem (in /usr/lib/system/libsystem_platform.dylib)
==11706== by 0x1547E2: stpcpy (in /usr/lib/system/libsystem_c.dylib)
==11706== by 0x1C2A43: __strcpy_chk (in /usr/lib/system/libsystem_c.dylib)
==11706== by 0x1000013CD: play (game.c:21)
==11706== by 0x1000012EF: main (starter.c:63)
==11706== Address 0x104000001 is not stack'd, malloc'd or (recently) free'd
==11706==
==11706==
==11706== Process terminating with default action of signal 11 (SIGSEGV)
==11706== Access not within mapped region at address 0x104000001
==11706== at 0x343040: _platform_memmove$VARIANT$Nehalem (in /usr/lib/system/libsystem_platform.dylib)
==11706== by 0x1547E2: stpcpy (in /usr/lib/system/libsystem_c.dylib)
==11706== by 0x1C2A43: __strcpy_chk (in /usr/lib/system/libsystem_c.dylib)
==11706== by 0x1000013CD: play (game.c:21)
==11706== by 0x1000012EF: main (starter.c:63)
==11706== If you believe this happened as a result of a stack
==11706== overflow in your program's main thread (unlikely but
==11706== possible), you can try to increase the size of the
==11706== main thread stack using the --main-stacksize= flag.
==11706== The main thread stack size used in this run was 8388608.
==11706==
==11706== HEAP SUMMARY:
==11706== in use at exit: 33,497 bytes in 377 blocks
==11706== total heap usage: 455 allocs, 78 frees, 39,513 bytes allocated
==11706==
==11706== LEAK SUMMARY:
==11706== definitely lost: 0 bytes in 0 blocks
==11706== indirectly lost: 0 bytes in 0 blocks
==11706== possibly lost: 0 bytes in 0 blocks
==11706== still reachable: 8,192 bytes in 2 blocks
==11706== suppressed: 25,305 bytes in 375 blocks
==11706== Rerun with --leak-check=full to see details of leaked memory
==11706==
==11706== For counts of detected and suppressed errors, rerun with: -v
==11706== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 133 from 34)
Segmentation fault: 11
如果没有提供可编辑的示例或遗漏任何相关信息,请发表评论,如果还有其他任何我需要提供的信息,请发表评论!
知道这里发生了什么吗?
谢谢!
答案 0 :(得分:0)
我认为问题在于你的初始化程序: 您遍历所有字段(行和列)并复制完整的行而不是单个元素。当你达到indice的上限时,你会复制超出数组的限制。
答案 1 :(得分:0)
初始化中的memcpy是错误。第三个参数应该是sizeof(master)而不是width-1。这假设数组1中元素的大小与master(数组2)中的元素大小相同
答案 2 :(得分:0)
这段代码不对:
void intialize(enum icons board[][WIDTH])
{
int i, j;
for(i=0; i < WIDTH; i++) {
for(j=0; j < WIDTH; j++) {
memcpy(&board[i][j], &master[i][j], WIDTH-1);
}
}
}
board[i][j]
的类型为enum icons
。使用memcpy
时,您需要使用sizeof(enum icons)
而不是WIDTH-1
。
你可以通过两种方式解决这个问题。
一次将master
的值分配给board
一个元素。
void intialize(enum icons board[][WIDTH])
{
int i, j;
for(i=0; i < WIDTH; i++) {
for(j=0; j < WIDTH; j++) {
board[i][j] = master[i][j];
}
}
}
或
使用memcpy
将数据行从master
复制到board
。
void intialize(enum icons board[][WIDTH])
{
int i;
for(i=0; i < WIDTH; i++) {
// Copy WIDTH enum icons from master to board.
memcpy(board[i], master[i], sizeof(enum icons)*WIDTH);
}
}