C++ 通过传递结构体似乎消除了初始化的需要。

huangapple go评论70阅读模式
英文:

C++ passing struct by seemingly removes need to initialize

问题

下面是已经翻译好的部分:

"我正在阅读Bjarne Stroustrup的《C++编程语言》一书,第2章的一些代码让我感到困惑。为了方便,在这篇帖子中,我缩短了书中的代码。

以下代码导致编译错误:“C4700 未初始化的本地变量'vector'被使用”。

struct Vector
{
    int size;
    double* elements;
};

void vector_init(Vector vector, int size)
{
    vector.elements = new double[size];
    vector.size = size;
}

int main()
{
    Vector vector;
    vector_init(vector, 3);
}

现在这对我来说是有道理的。让我感到困惑的是,如果我改为将Vector结构体作为引用传递,错误就消失了。

struct Vector
{
    int size;
    double* elements;
};

void vector_init(Vector& vector, int size)
{
    vector.elements = new double[size];
    vector.size = size;
}

int main()
{
    Vector vector;
    vector_init(vector, 3);
}

以这段代码为起点,我已经能够填充vector结构体并打印出其值。我不明白为什么通过引用传递它意味着我不再需要初始化结构体。"

英文:

I'm reading the book "The C++ Programming Language" by Bjarne Stroustrup, and some of the code in chapter 2 has me confused. I've shortened the code from the book for the sake of this post.

The following code causes a compilation error "C4700 uninitialized local variable 'vector' used".

struct Vector
{
	int size;
	double* elements;
};

void vector_init(Vector vector, int size)
{
	vector.elements = new double[size];
	vector.size = size;
}

int main()
{
	Vector vector;
	vector_init(vector, 3);
}

Now this makes sense to me. What doesn't make sense to me, is that if I instead pass the Vector-struct as a reference, the error goes away.

struct Vector
{
	int size;
	double* elements;
};

void vector_init(Vector& vector, int size)
{
	vector.elements = new double[size];
	vector.size = size;
}

int main()
{
	Vector vector;
	vector_init(vector, 3);
}

Using this code as a starting point I've been able to populate the vector-struct and printed out its values. I fail to understand why passing it by reference means I now longer need to initialize the structure.

答案1

得分: 5

在你的第一个示例中,对 vector_init(vector, 3) 的调用会 复制 vector 并传递给 vector_init,但 vector 尚未初始化。由于程序需要从 vector 中读取以进行复制,而从未初始化的变量中读取会导致未定义的行为,因此该程序的行为是未定义的。

相比之下,在你的第二个示例中,从未初始化的变量中根本没有读取任何内容。由于 vector_init 通过引用接受其参数,因此无需进行复制。对未初始化的变量进行 写入 是被定义良好的,因此结果是一个良好定义的程序,并且 vector 的成员具有良好定义的值。

英文:

In your first example, your call to vector_init(vector, 3) makes a copy of vector to pass to vector_init, but vector is uninitialized. Since the program needs to read from vector in order to copy it, and reading from uninitialized variables results in undefined behavior, that program's behavior is undefined.

In contrast, in your second example nothing is ever read from an uninitialized variable. Since vector_init takes its argument by reference, no copy needs to be made. Writing to an uninitialized variable is well-defined, so the result is a well-defined program and well-defined values for the members of vector.

答案2

得分: 2

"warning"消失是因为未初始化的Vector没有被读取。您只为其成员分配值。

在第一个示例中,未初始化的Vector被复制(使用/读取),导致_未定义的行为_。

英文:

> I instead pass the Vector-struct as a reference, the error goes away.

The warning goes away because the uninitialized Vector isn't read. You only assign values to its members.

In the first example, the uninitialized Vector is copied (used/read), causing undefined behavior.

huangapple
  • 本文由 发表于 2023年7月10日 22:27:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定