-1

I come across an example of a function as below:

int someFunction(void)
{
    int i;

    for(i = 0; i < 10; i++)
    {           
            a_var[i] = 0xFFFF; //uninitialize a_var
    }

    return 0;
}

What is the purpose of uninitialize a variable and why is it have to use 0XFFFF?

danteDev
  • 35
  • 7
  • 5
    I believe this is simply a convention used by the author of the code. – Andrzej Pronobis Aug 03 '16 at 00:14
  • 3
    This isn't really uninitializing a_var[i] is it? – Tdorno Aug 03 '16 at 00:14
  • 5
    There is no such thing as uninitializing a variable. The closest thing would be cleaning up resources like allocated memory and open file handles, but this code doesn't do anything like that. – user2357112 Aug 03 '16 at 00:15
  • Clearing a variable like this can be used to check for memory violations. – john elemans Aug 03 '16 at 00:17
  • user2357112 is correct. No such thing as unitializing a variable. Just cleaning up resources. However I believe all this is doing is setting all the bits that a_var can hold to 1, since F = 1111. a_var[i] might be some flag or checksum and this might be useful for those scenarios. – Omid CompSCI Aug 03 '16 at 00:20
  • The only person that can give an exact answer to your question is the author of that code. But the common reason is that `0xFFFF` is not a valid value that is expected to be contained in the `a_var` array so is used to mark those entries as not in use. Note that the chosen value may be different in different contexts. – kaylum Aug 03 '16 at 00:22
  • @johnelemans: Yes, but yuck. There are proper tools for that purpose that do a much better job (e.g. ASAN or Valgrind). – Kerrek SB Aug 03 '16 at 00:26
  • @KerrekSB and gdb? – Omid CompSCI Aug 03 '16 at 00:27

2 Answers2

5

Code is not un-initializing a variable. Code is simply setting the variable to the value 0XFFFF or 65535. @user2357112

The author of the code may be using this value (16 one bits) to signify something special - but that is application specific. There is no general un-initializing a variable in C. @And

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • The type of the array is not specified (that's why we ask for a [mcve]). So we don't know if the variable really will have `65536`. Considering the wrong comment and apparently using a global array and uselessly returning a result, it can be `-1` or other signed representation. – too honest for this site Aug 03 '16 at 11:51
  • @Olaf, did you mean 65535 and not 65536? Even if code is `char x = 65535`, code is still setting the variable to 65535. Agree the result of that setting (assigning) depends of the target variable type. – chux - Reinstate Monica Aug 03 '16 at 11:58
  • I tend to interpret the assignment in context of the LHS, because that's what the object is set to. And because the RHS is irrelevant after the assignment. – too honest for this site Aug 03 '16 at 12:30
1

As said by others, there is no such thing as "un-initialising" a variable. That comment is misleading.

What the author probably meant is: let's set it to a value that clearly stands out (for example in the debugger) and that is not meant to be used by a program that runs correctly; instead, it is meant to be overwritten when the proper initialisation takes place. If the program crashes and the debugger shows the value is 0xFFFF, it means the variable hasn't been initialised properly (otherwise it would have a different value), and then you know your code is accessing it at the wrong time, before the "correct" initialisation. Or, similarly, it could be that this function is called when the variable is not meant to be used again, and seeing that value indicates that the variable has been used after its intended "end of life".

This technique is applied by Visual Studio when debugging C++ code: the variables are all automatically initialised with special values, chosen to be easy to see and remember. Examples include 0xABABABAB, 0xABADCAFE and 0xDEADDEAD. You can find more here and here. Wikipedia has a much longer list.

The author of this program is probably trying to replicate it. A better comment would be: "Set a_var to a known wrong value that helps detecting improper early/late accesses to it".

Community
  • 1
  • 1
  • I guess this is the closest possible scenario. For the purpose of debugging and detect problem that will either change the 0xFFFF value or has been changed to other value. – danteDev Aug 03 '16 at 04:18
  • For debugging, `0xFFFF` is a weak choice -only slightly better than `0x0000`. For debugging, more useful that the _un-initializied_ value contain a uncommon bit pattern. Thus I doubt author is using for debugging but to set to a special state that the application deems as "un-initializied". – chux - Reinstate Monica Aug 03 '16 at 11:09
  • There is no initiailisation either. But an assignment. – too honest for this site Aug 03 '16 at 11:49
  • Ok, but still there is too much speculation. The question does not provide enough context. – too honest for this site Aug 03 '16 at 12:32
  • @Olaf, a_var is type uint16. The variable value will be updated in other c file, it is just a pure data. The author just define it with 0xFFFF for the first time in the program and comment it as uninitialize. So, the question is why have to do this, is it a common practice in writing a program. – danteDev Aug 04 '16 at 00:50
  • @Olaf, I don't see your comment related to the question. Thanks for your input anyway. – danteDev Aug 04 '16 at 04:11