为什么内存会被破坏?

时间:2014-11-28 11:36:53

标签: c memory

我有以下2个功能:

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个值是垃圾,我真的不喜欢明白为什么。我编写了很多虚拟示例来填充函数内的各种数组,它们似乎都可以工作。

在第二步,当我真的感到沮丧时,我只是用我知道应该包含的值填充bytes_in_stream,这样我就可以进入代码的下一部分了。令我惊讶的是,当我尝试打印encr_streams时,我的程序在i变为24时崩溃(总共有31 encr_streams)。

我真的不明白为什么会发生这种情况,而且我觉得我处于一种未定义行为的情况。

当程序找到key_length6时,对于另一个流,该程序运行顺畅。

1 个答案:

答案 0 :(得分:2)

这一行错了:

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

应该是:

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