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

前端 未结 4 1231
孤街浪徒
孤街浪徒 2020-12-19 21:05

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

相关标签:
4条回答
  • 2020-12-19 21:18

    The behavior must be identical, but any differences in the generated code really depend on the compiler.

    For example, the compiler could generate this for the initialized variable:

    somefunction:
    pushl    %ebp
    movl     %esp, %ebp
    pushl    $2 ; allocate space for x and store 2 in it
    ...
    

    and this for the uninitialized, but later assigned variable:

    somefunction:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $4, %esp ; allocate space for x
    ...
    movl    $2, -4(%ebp) ; assign 2 to x
    ...
    

    The C standard does not mandate the generated code to be identical or non-identical in these cases. It only mandates identical behavior of the program in these two cases. And that identical behavior does not necessarily imply identical machine code.

    0 讨论(0)
  • 2020-12-19 21:19

    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.

    0 讨论(0)
  • 2020-12-19 21:24

    An important distinction comes into play when you add a const qualifier:

    int const x = 2;
    

    is valid C

    int const x;
    x = 2;
    

    isn't. Another important difference is for static variables:

    static int x = f();
    

    is invalid C

    static int x;
    x = f();
    

    is valid.

    0 讨论(0)
  • 2020-12-19 21:33
    int x = 2;
    

    Computer will create variable x and assign to it value 2 almost at the same moment.


    int x;
    x = 2;
    

    Computer will create variable x. And then it will it assigned to it value 2. It seem that there is no any difference, but...

    ...let's suppose that your code is like this:

    int x; 
    {some operators};
    x = 2; 
    

    computer may have to access the variable x in order to assign to it value 2. It means that while running program computer will spend more time to access x to assing to it some value unlike if it will create variable and assing this variable at the moment.

    Anyway, Deitel HM, Deitel PJ describe this in C How to Program.

    0 讨论(0)
提交回复
热议问题