c - Memcpy 导致 strcpy 中出现段错误?包括 Valgrind 输出

标签 c memory valgrind memcpy strcpy

我尝试复制我在项目中遇到的这个错误,但所有努力都失败了,我的复制编译和测试完美。

基本上,我不断收到一个段错误,该错误可以追溯到对 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

很抱歉没有提供可编译的示例,或遗漏了任何相关信息,如果我需要提供其他任何内容以使内容更清晰,请发表评论!

知道这里发生了什么吗?

谢谢!

最佳答案

这段代码不正确:

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] 的类型是枚举图标。使用memcpy时,需要使用sizeof(enum icon)而不是WIDTH-1

您可以通过两种方式解决此问题。

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

  2. 使用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);
       }
    }
    

关于c - Memcpy 导致 strcpy 中出现段错误?包括 Valgrind 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25489508/

相关文章:

c - 难以理解MACRO的偏移量

c - 抑制 lint 消息 9007

c - 有没有办法获取短路数据的最后一个字节并将其保存到字符中

c - 学习 C 试图通过函数找出这个 malloc 的东西

c - 给定集合的幂集

Android 内存不足,原因不明

c - 在 C 中为结构内的指针分配和分配内存?

c++ - 使用 Valgrind 输出调查内存泄漏

c - Valgrind 使用 g_test_trap_subprocess () 给出可能丢失的内存

c++ - 使用 Valgrind 检查时,Libzip 示例包含未初始化的值