简体   繁体   English

fill_n()不允许更改变量值

[英]fill_n() is not letting change variable value

I am using fill_n() function to initialize my array values, but then I cannot change the values of the array, it is always the initial value. 我正在使用fill_n()函数初始化我的数组值,但是后来我无法更改数组的值,它始终是初始值。

Can someone explain me why is that? 有人可以解释我为什么吗?

#include<iostream>
#include<ctime>


int main(){

    //Matrix 
    int m[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
    int v[3] = {5,6,7}; 
    int result[3];// = {0,0,0};
    int n = 0, i, j;

    std::fill_n(result,sizeof(result),0);

    clock_t time = clock();

    while(n<1000){
        n++;
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                result[j] = result[j]+(v[i]*m[j][i]); //changing the values here.
            }
        }
    }

    time = clock() - time;

    for(i=0;i<3;i++){
    std::cout<<result[i]<<"\n";
    }

    std::cout<<"Execution time: "<<((float)time)/CLOCKS_PER_SEC<<"\n";

    return 0;
}

The output of the code is always 0. 代码的输出始终为0。

By std::fill_n(result,sizeof(result),0); 通过std::fill_n(result,sizeof(result),0); you are asking to fill the first sizeof(result) elements by 0. 您要求用0填充第一个sizeof(result)元素。

However, only 3 elements are available in result . 然而,只有3个元素是可用的result Thus you wrote out-of-bound and led to undefined behavior. 因此,您越界书写并导致未定义的行为。 You should write std::fill_n(result, sizeof(result) / sizeof(result[0]), 0); 您应该编写std::fill_n(result, sizeof(result) / sizeof(result[0]), 0); instead. 代替。

Note that sizeof(result) returns size of result in bytes , not in elements . 需要注意的是sizeof(result)的大小返回result字节为单位 ,而不是在元件

sizeof(result) returns the number of bytes held by the variable result . sizeof(result)返回变量result持有的字节数。 Since result is an array of 3 int s, its size in memory is 3 * sizeof(int) , which (unless sizeof(int) is 1 ) much greater than 3 . 由于result是一个3 int s的数组,因此其在内存中的大小为3 * sizeof(int) ,(除非sizeof(int)1 )比3大得多。 Thus you are writing pass array bounds and incurring undefined behavior. 因此,您正在编写传递数组边界并引起未定义的行为。

The actual way to calculate the size would be to divide the entire size of result by its element type. 计算大小的实际方法是将result的整个大小除以其元素类型。 That is, sizeof(result) / sizeof(int) . 也就是说, sizeof(result) / sizeof(int)

If you want to initialize each element to 0 , a simpler way to do it would be to value-initialize the array: 如果要将每个元素初始化为0 ,则更简单的方法是对数组进行值初始化

int result[3]{}; // or = {} (pre C++11)

Number of elements (or size) of an array of type type would be sizeof( array ) / sizeof( type ). 类型类型 数组的元素数(或大小)将为sizeof( array )/ sizeof( type )。 Second argument required for fill_n is the number of elements to fill a container (in your case, an array of type int) and not the size of an array in bytes. fill_n所需的第二个参数是填充容器的元素数(在您的情况下为int类型的数组),而不是数组的大小(以字节为单位)。 Naturally, sizeof( array ) returns sizeof( type ) times number of elements. 自然,sizeof( array )返回sizeof( type )乘以元素数。

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

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