Dear professionals, do I need to initialize the pointer with a NULL value before adding the address to it to a variable? That is, is it necessary to assign an address?

int x = 80; int *num; num = &x; 

or so?

 int x = 80; int *num = NULL; num = &x; 

    3 answers 3

    It is not necessary to initialize, like any other variable.

    If, of course, you do not use an uninitialized variable before it is assigned the correct value.

    But initialization is just a good tone, self-control and a way to avoid mistakes due to use before initialization. It’s better not to neglect it :)

    A good tone also after the release of memory zeros variable pointer - to at least avoid accidental double release.

    • It seems to me, or "at least" in this case represents "only"? Veda reading a null pointer is not much better than reading a freed one. Although usually crashes ... - Qwertiy ♦
    • 2
      @Qwertiy Well, many functions track if a null pointer is passed to them. So, for example, passing a pointer to the freed memory can lead to trouble, and zero will be a regular situation. Further, referring to the null pointer still more often leads to immediate crash, as opposed to writing to an invalid address, which may appear much later (and we don’t even have to read about reading ...). - Harry
    • Well, yes, perhaps. - Qwertiy ♦

    The question is somewhat meaningless - both options are bad.

    First, in all cases, you should try to follow the rule to declare variables as locally as possible. One of the main advantages (if not the main advantage) of such an approach to declaring variables is the ability to immediately initialize the declared variable with the correct value, meaningful from the point of view of the implemented algorithm.

    In your case it will be

     int x = 80; int *num = &x; 

    those. The question of some kind of "initialization before assignment" should not be in sight. Without extreme necessity, no division into preliminary initialization and assignment in the code should arise at all.

    Secondly, in the same cases when, for one reason or another, at the point of declaring a variable it fails to immediately assign a meaningful initializer, it is necessary to act according to circumstances. Universally correct absolutely applicable rules do not exist. But we can immediately say that the indiscriminate appointment of all variables of abstract "dummy" initializers in a situation where there is no meaningful initializer is the roughest and most dangerous anti-pattern, which, unfortunately, is often implanted in the form of a textbook cargo cult in the immature minds of students. All that happens in the case of such initialization is the replacement of an undefined "garbage" value with some specific "garbage" value. Such a replacement does nothing useful, but can "sweep under the carpet" errors in the code, make it difficult to read the code, interfere with optimizations and suppress the built-in sanitization tools of the debugging code that almost all modern compilers have. The latter in many cases (in most cases) easily catch and report access to an uninitialized variable, but for obvious reasons they cannot catch access to a variable formally initialized by some "dummy" value.

    Therefore, the general recommendation here is this: initialization is very good. But it should not be initialization purely for the sake of initialization "whatever it is." If possible, try to structure your declarations so that each variable declaration is immediately accompanied by its content initialization, i.e. exactly the value that you are going to “assign” to it (in the terminology of the original question). If such a meaningful initialization is impossible, then in most cases, it may be better to leave the variable uninitialized altogether than to initialize it purely for the sake of some kind of "default" value (such as zero).

    There is even such a practice when in a situation when there is no ready initializer for a declared variable, it is deliberately initialized with a deliberately “curved” value, such as, for example, (void *) 0xBAADF00D for pointers. Such an "initialization" increases the likelihood that accessing an actually uninitialized variable will cause an instant program crash, i.e. will be detected earlier and not later. A null pointer, unlike (void *) 0xBAADF00D , increases the likelihood that a program error will live unnoticed for a long time.

    • But what do you think is meaningless in the question? - Excess Gophers
    • @ DuuudeXX8: I wrote: that both options are wrong. The question actually puts us before a choice: one way or another. This choice is meaningless, because it is not right and not so. - AnT
    • thank you very much for the clear explanation - Excess
    • And DEADBEEF is no longer politically correct? - avp
    • @avp: DEADBEEF , as well as BAADF00D - fragments of moss past, lovely only to the heart of the stagnant retrogrades! Iron 64-bit horse comes to replace the 32-bit peasant horse! And there is an incomparably wider scope for creativity: BAADF00DDEADBABE , etc. - AnT

    If you immediately assign a value, then the easiest / best way I think is to do this:

     int *num = &x; 

    And about whether you can or not: C believes that you are an adult and understand what you write. Language will not bother you to do wrong. A variable can be not initialized if you are guaranteed to assign a value to it before the first reading. It is impossible to read an uninitialized variable, and you, the programmer, are responsible for implementing this rule.