简体   繁体   English

为什么0是我动态创建的数组的最后一个值的一个非常常见的整数?

[英]Why is 0 a very common integer for the last values of my dynamically created array?

I am currently in the process of writing a simple function to compute the sum of two vectors. 我目前正在编写一个简单函数来计算两个向量的总和。 Being that I am new to C, I figured I would generate some "psuedo-random" values for my vectors and to also randomize their length - which is more than needed for this exercise. 因为我是C的新手,我想我会为我的矢量生成一些“伪随机”值,并随机化它们的长度 - 这比本练习需要的更多。

Basically, I just started writing my code and wanted to see what the values of my array happen to be. 基本上,我刚开始编写代码,想看看我的数组的值是什么。

Here's the code: 这是代码:

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


int vectorSum(int x[], int y[], int n, int sum[]) {
}

void display_Array(int *a, int len){
     //Declerations
     int i;
     for(i=0; i<len; i++)
     {
          printf("%d ", a[i]);
     }
     printf("\n");    
 }

//Implement a nice randomizing function...Fisher-Yates shuffle

int main(void){
    //Generate a random length for the array
    srand(time(NULL));
    int N = (rand()*rand())%100;

    //Declarations
    int *x = (int *) malloc(N * sizeof(*x)); //Because I don't know how large my array is ahead of time
    int *y = (int *) malloc(N * sizeof(*y));
    int i;

    //Fill the arrays
    for(i=0; i<10; ++i) {
         x[i] = (rand()*rand())%100000;
         y[i] = (rand()*rand())%100000;
    }

    display_Array(x, N);
    getchar();


    //Main execution...



}

Note that this code is no where near being finished - feel free to critique as much as you wish. 请注意,此代码并非即将完成 - 您可以随意批评。 I am aware it is far from polished. 我知道它远非抛光。 However, I noticed something odd. 但是,我注意到一些奇怪的事。 My output would often follow this pattern: 我的输出通常会遵循这种模式:

w, x, y...z, 0, 0, 0, 0, 0, 0, 0, 0.

More often than not, the last values in my array would be 0. Not always, but more often than not. 通常情况下,我的数组中的最后一个值将是0.并非总是如此,但往往是。 Furthermore, this pattern usually occurs in multiples. 此外,这种模式通常以倍数出现。 For instance, the last two values are 0, 0, then I run the program again, the last four digits are 0, 0, 0, 0, and so forth. 例如,最后两个值是0,0,然后我再次运行程序,最后四位数字是0,0,0,0等。

So, why does this happen? 那么,为什么会这样呢? I have my suspicions, but I would like to hear other's thoughts. 我怀疑,但我想听听别人的想法。

C does not initialize the values of the array. C不初始化数组的值。 So what you find in the cells that have not been set is simply the content of non-erased memory (and as you loop through all allocated cells, and set values only of first 10 ones, there are lots of them). 所以你在未设置的单元格中找到的只是非擦除内存的内容(当你循环遍历所有已分配的单元格,并且只设置前10个单元格的值时,它们就有很多)。 If your memory was "clear", then you will most probably see zeros, but if there are some garbage from previous iterations/other programs - you can get any kind of values. 如果你的记忆是“清晰的”,那么你很可能会看到零,但如果前一次迭代/其他程序中有一些垃圾 - 你可以获得任何类型的值。

Change this line of code: 更改此行代码:

 for(i=0; i<10; ++i)

to

for(i=0; i<N; ++i)

Besides, don't use rand() * rand() , it will create integer overflow. 此外,不要使用rand() * rand() ,它会创建整数溢出。 Instead use just rand() . 而是只使用rand()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM