c - C 中奇怪的内存损坏

标签 c clang

在回答另一个问题时,我写了一些简单的代码来初始化和打印二维数组。但一些非常奇怪的事情正在发生。

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

void print_row( const int *row, size_t num_cols ) {
    printf("%p\n", (void*)row);
    for( size_t col_num = 0; col_num < num_cols; col_num++ ) {
        printf(" %2d ", row[col_num]);
    }
    puts("");
}

int **make_board( const size_t num_rows, const size_t num_cols ) {
    int **board = malloc( sizeof(int) * num_rows );

    for( size_t row_num = 0; row_num < num_rows; row_num++ ) {
        int *row = calloc( num_cols, sizeof(int) );
        board[row_num] = row;
        print_row(row, num_cols);
    }

    return board;
}

void print_board( int **board, const size_t num_rows, const size_t num_cols ) {
    for( size_t row_num = 0; row_num < num_rows; row_num++ ) {
        const int *row = board[row_num];
        print_row(row, num_cols);
    }
}

int main() {
    size_t num_rows = 6;
    size_t num_cols = 4;
    puts("Making the board");
    int **board = make_board(num_rows, num_cols);
    puts("Printing the board");
    print_board(board, num_rows, num_cols);
}

运行它时,我偶尔会遇到一行损坏的情况,但只出现在 print_board 中,而从未出现在 make_board 中。

cc -Wall -Wshadow -Wwrite-strings -Wextra -Wconversion -std=c99 -pedantic -g   -c -o test.o test.c
cc   test.o   -o test

Making the board
0x7fc4e6d00370
  0   0   0   0 
0x7fc4e6d001e0
  0   0   0   0 
0x7fc4e6d001f0
  0   0   0   0 
0x7fc4e6d00200
  0   0   0   0 
0x7fc4e6d00210
  0   0   0   0 
0x7fc4e6d00220
  0   0   0   0 
Printing the board
0x7fc4e6d00370
  0   0   0   0 
0x7fc4e6d001e0
 -422575600  32708  -422575584  32708 
0x7fc4e6d001f0
  0   0   0   0 
0x7fc4e6d00200
  0   0   0   0 
0x7fc4e6d00210
  0   0   0   0 
0x7fc4e6d00220
  0   0   0   0 

如果我链接到包(例如 glib-2),损坏会更频繁地发生。

cc -Wall -Wshadow -Wwrite-strings -Wextra -Wconversion -std=c99 -pedantic -g `pkg-config --cflags glib-2.0`   -c -o test.o test.c
cc `pkg-config --libs glib-2.0`   test.o   -o test

内存位置全部正确。初始化期间所有行都正常。没有编译器警告。 -fsanitize=address 未发现任何错误。

什么可能导致这一行损坏?有人能重复这个问题吗?

$ cc --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin17.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ uname -a
Darwin Windhund.local 17.6.0 Darwin Kernel Version 17.6.0: Tue May  8 15:22:16 PDT 2018; root:xnu-4570.61.1~1/RELEASE_X86_64 x86_64 i386 MacBookPro8,1 Darwin

最佳答案

您将 int 指针数组分配为 int 数组,从而导致一些意外行为。幸运的是,修复很简单。

替换这一行:

int **board = malloc( sizeof(int) * num_rows );

用这一行:

int **board = malloc( sizeof(int *) * num_rows );

或者,为了避免将来出现此类错误,正如 Jonathan Leffler 在下面的评论中指出的那样,您可以对尝试分配的取消引用变量执行 sizeof 运算符,这样您就可以不必担心类型是否正确:

int **board = malloc( sizeof(*board) * num_rows );

关于c - C 中奇怪的内存损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50957919/

相关文章:

c - 使用C程序从外部打开文件

c - 我的字符数组在 C 中合并

c - 如何打印 short int C 对象的字节表示

c++ - LLVM 无法从 IR 编译

iOS 14 链接模块标志 'Dwarf Version' : IDs have conflicting behaviors

c - OS X Xcode/clang 构建 Windows 可执行文件?

C:识别变量类型

c++ - LibClang clang_getArgType() 返回错误的类型

visual-studio-2012 - 如何为 vcxproj 中的每个编译单元提取编译参数?

c - 在 llvm 链接 *.a 和 *.o 文件时出错