English 中文(简体)
为什么C程序崩溃?
原标题:Why is this C program crashing?
  • 时间:2012-05-24 21:35:51
  •  标签:
  • c
  • crash

我在 C 中有一个简单的测试程序, 以调和堆积上的一系列值。 主题 : 我知道随机逻辑有一个缺陷, 不允许“ 迁移” 值超过 < code> RAND_ MAX , 但这不是此文章的要点 。

要点是当我用 N = 10000 运行代码时, 它会偶尔在很少信息的情况下崩溃( 屏幕截图张贴在下面 ) 。 I m 使用 MINGW 编译器。 我似乎无法复制此崩溃的 N值更低或更高( 比如1000 或 100000 ) 。

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

const int N = 10000;

int main() {
    int i, rand1, rand2, temp, *values;

    /* allocate values on heap and initialize */
    values = malloc(N * sizeof(int));
    for (i = 0; i < N; i++) {
        values[i] = i + 1;
    }

    /* scramble */
    srand(time(NULL));
    for (i = 0; i < N/10; i++) {
        rand1 = (int)(N*((double)rand()/(double)RAND_MAX));
        rand2 = (int)(N*((double)rand()/(double)RAND_MAX));
        temp = values[rand1];
        values[rand1] = values[rand2];
        values[rand2] = temp;
    }


    int displaced = 0;
    for (i = 0; i < N; i++) {
        if (values[i] != (i+1)) {
            displaced++;
        }
    }
    printf("%d numbers out of order
", displaced);

    free(values);
    return 0;
}

""https://i.sstatic.net/DT0GA.png" alt="此处输入图像描述"/"

最佳回答

这可能是因为 rand () 产生一个从0到RAND_MAX的随机数字 包含 的随机数字,因此 (int)(N* ((双)rand()/(双)RAND_MAX)) 可以是N,该数字超过数组边界。然而,我不明白为什么这种数字会随数组大小而变化(它确实解释了为什么它只是偶尔碰撞,尽管如此)。

尝试 /( 1+( 双) RAND_ MAX) (注意添加值是双倍, 以避免溢出, 取决于RAND_ MAX的值)( 虽然我并不确信这总是有效的, 取决于所涉及的类型。 测试 N 比较安全, 然后再试一次 ) 。

学习从< a href=' https://stackoverflow.com/ questions/413477/is- there-a-good-valgrind-subtitution- for- windows' 中学习使用一个工具吗? - 他们让这种事情很容易解决(他们告诉你,当你运行您的程序时到底出了什么问题)。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签