Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

c - Is there a difference between initializing a variable and assigning it a value immediately after declaration?

Assuming a purely non-optimizing compiler, is there any difference in machine code between initializing a variable and assigning it a value after declaration?

Initialization method:

int x = 2;

Assignment method:

int x;
x = 2;

I used GCC to output the assembly generated for these two different methods and both resulted in a single machine instruction:

movl    $2, 12(%esp)

This instruction just sets the memory held by the x variable to the value of 2. GCC may be optimizing this because it can recognize the end result of the operations; but I think this is the only way to interpret the two versions. My reasoning is that both version do the same thing: set a part of memory to a specific value.

Why is it then that a distinction is often made between the terms "initialization" and "assignment" if the resulting machine code is the same?

Is the term "initialization" used purely to differentiate variables which have a specific value assigned over those (non-initialized) variables which have whatever garbage value was left in memory?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming a purely non-optimizing compiler, is there any difference in machine code between initializing a variable and assigning it a value after declaration?

Sure.

  • char fubar[] = "hello world"; is valid.
  • char fubar[]; fubar = "hello world"; is not.

More?

  • int fubar[128] = { [60] = 42 }; is valid.
  • int fubar[128]; fubar = { [60] = 42 }; is not.

More?

  • struct foo bar = { .foo = 13, .bar = 42 }; is valid.
  • struct foo bar; bar = { .foo = 13, .bar = 42 }; is not.

More?

  • const int fubar = 0; is valid.
  • const int fubar; fubar = 0; is not.

I could go on and on... Hence, machine code might exist for one while it most likely won't for the other. On that note, have you ever heard of an implementation of C that isn't a compiler?

Why is it then that a distinction is often made between initialization and assignment if the resulting machine code is the same?

The concept of variables in the C programming language is too high-level for the low-level machine code representation. In machine code, registers don't have scope. C added scope, not to mention type fusion and many of the other variable-related aspects, along with initialisation (which you can see from previous examples is squarely, but unfortunately not the same).

Is the term "initialization" used purely to differentiate variables which have a specific value assigned over those (non-initialized) variables which have whatever garbage value was left in memory?

Though a variable that is "initialized" won't contain any "garbage value" (or trap representations), this is not the only affect it has.

In my first example, the initialization will provide the size of the otherwise incomplete array. The equivalent using the assignment operator would require explicitly providing the length of the array and using strcpy, which turns out to quite tedious.

In my second example, the int at index 60 will be initialized to 40 while the remaining, otherwise uninitialized items will be initialized to 0. The equivalent using the assignment operator would also be fairly tedious.

In my third example, the members foo and bar will be initialized to 13 and 42 while the remaining, otherwise uninitialized members will be initialized to 0. The equivalent using the assignment operator would be quite tedious, though I occasionally use a compound literal to achieve a similar result.

In my fourth example, the initialization sets the value that the variable will contain for it's entire life. No assignment is possible to this variable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...