首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中的奇怪内存损坏

C中的奇怪内存损坏
EN

Stack Overflow用户
提问于 2018-06-20 22:27:19
回答 1查看 108关注 0票数 2

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

代码语言:javascript
复制
#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中。

代码语言:javascript
复制
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,腐败就会发生得更频繁。

代码语言:javascript
复制
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没有发现错误。

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

代码语言:javascript
复制
$ 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 22:35:44

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

替换这一行:

代码语言:javascript
复制
int **board = malloc( sizeof(int) * num_rows );

用这一行:

代码语言:javascript
复制
int **board = malloc( sizeof(int *) * num_rows );

或者,为了避免将来发生这类错误,乔纳森·莱弗勒在下面的注释中指出,您可以在试图分配的取消引用变量上执行sizeof操作符,这样您就不必担心类型是否正确:

代码语言:javascript
复制
int **board = malloc( sizeof(*board) * num_rows );
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50957919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档