c - 为什么内存会损坏?

标签 c memory

我有以下两个功能:

void compute_key(uint8_t * stream, int key_length, int stream_length, uint8_t * key) {

    // there will be key_length encrypted streams;
    uint8_t ** encr_streams;
    int * bytes_in_stream;
    encr_streams = (uint8_t **)malloc(key_length * sizeof **encr_streams);
    bytes_in_stream = (int *)malloc(key_length * sizeof *bytes_in_stream);
    for (int i = 0; i < key_length; i++) bytes_in_stream[i] = 0;

    construct_cypherstreams(stream, key_length, stream_length, encr_streams, bytes_in_stream);

    printf("%s\n", "bytes_in_stream[]");
    for (int i = 0; i < key_length; i++) {
        printf("%d\n", bytes_in_stream[i]);
    }

    getchar();

    for (int i = 0; i < key_length; i++) bytes_in_stream[i] = 7; // introduced in step #2
    printf("\n%s\n\n", "cypherstreams:");
    for (int i = 0; i < key_length; i++) {
        for (int j = 0; j < bytes_in_stream[i]; j++) {
            printf("%X", encr_streams[i][j]);
        }
        printf("\n\n");
    }


    // for each cypherstream, compute ByteB - the value they were XOR`d with
    compute_key_using_scoring(encr_streams, bytes_in_stream, key_length, key);

    getchar();
}
void construct_cypherstreams(uint8_t * stream, int key_length, int stream_length, uint8_t ** encr_streams, int * bytes_in_stream) {
    // chyperstream = the stream formed of every ith byte
    uint8_t * cypherstream;
    int length;
    length = stream_length / key_length + 1;
    // each byte of the key can have values
    // between 0 and 256
    int i = 0;
    int num_added = 0;
    for (int k = 0; k < key_length; k++) {
        i = k; num_added = 0;
        cypherstream = (uint8_t *)malloc(length * sizeof *cypherstream);
        if (cypherstream == NULL) {
            printf("%s\n", "could not allocate");
            exit(1);
        }
        while (i < stream_length) {
            // construct cypherstream
            cypherstream[num_added] = stream[i];
            num_added++;
            i += key_length;
        }
        // this is always correct
        printf("\n%s\n", "created cypherstream:");
        for (int m = 0; m < num_added; m++) {
            printf("%X", cypherstream[m]);
        }
        printf("\n");
        encr_streams[k] = cypherstream;
        bytes_in_stream[k] = num_added;
        // this is always correct
        printf("%s%d%s %d\n", "bytes_in_stream[", k, "]", bytes_in_stream[k]);
    }
}

我的key_length31,我确信它很好(之前计算过,也适用于其他值,对于这些值,程序到达末尾并打印解密的 chypertexht)。

construct_cypherstreams中,我将指针传递给compute_key中动态分配的内存。 我的目的是在编译时填充未知大小的 2D 数组 encr_streams 和 1D 数组 bytes_in_stream

construct_cypherstreams 中,我在每次迭代后打印 bytes_in_stream[k] 的内容,并获得有效数据。每个流有 7 个字节,一切都按预期工作。我还打印了新形成的 encr_streams[k] 并且也得到了预期值。

在第 1 步,程序从 construct_cypherstreams 返回后,我检查 bytes_in_stream 的内容是否正确,并且我看到前 7 个值是垃圾我真的不明白为什么。我已经编写了很多虚拟示例来填充函数内的各种数组,它们似乎都有效。

在第 2 步,当我感到非常沮丧时,我只是用我知道它应该包含的值填充 bytes_in_stream ,以便我可以进入代码的下一部分。令我惊讶的是,当我尝试打印 encr_streams 时,当 i 变为 24 (总共 31 个 encr_streams)。

我真的不明白为什么会发生这种情况,我感觉我遇到了未定义行为的情况。

当发现另一个流的 key_length6 时,程序运行顺利。

最佳答案

这一行是错误的:

encr_streams = (uint8_t **)malloc(key_length * sizeof **encr_streams);

应该是:

encr_streams = (uint8_t **)malloc(key_length * sizeof *encr_streams);

关于c - 为什么内存会损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27187818/

相关文章:

c - 将 cobol s9(2) COMP 检索到 C 变量

c++ - unsigned 关键字可以以非显而易见的方式使用吗?

c - 根据偏移量和长度设置数组中的 bin 字段

python - 由 Python 多处理 fork 的空 python 进程的私有(private)内存

memory - 用short替换int对CUDA的性能有帮助吗

c - 为什么这个函数有段错误?

Linux静态代码分析工具对比?

performance - mongodb更新导致读取速度极慢

Android Studio - 旋转设备导致使用越来越多的内存

c - 从缓冲区分配不同的内存块