Mr. Branch
Mr. Branch

Reputation: 442

Is there anyway to know if a variable has been initialized in C?

I'm currently working on a program which picks up input(via main) and draws different fractals depending on the input. I'm saving the parsed and converted(to a number)user input, in a structure:

typedef struct
{
        unsigned int xcord,ycord;
}point_t;

typedef struct
{
        int fractalType;
        double lstart,lend,lconstant;
        double leftangle,rightangle;
        point_t drawStart;

}input_data_t;

The problem I'm having is that certain fractals don't use all the variables contained in the structure, and if a certain fractal is called with a variables it doesn't use I must show an error.

Anyways now on to the problem, I know that variables when DECLARED "pick up " garbage from what was before hand in the assigned memory position. Is there any way to know if a variable has been initialized during run time, as to insure after parsing that no unnecessary variable has been used? (If so I need a cross platform solution)

Upvotes: 0

Views: 350

Answers (3)

zzn
zzn

Reputation: 2465

why not just initialize that struct with some invalid value, then the problem become whether the value is invalid.

Upvotes: 0

5gon12eder
5gon12eder

Reputation: 25439

No, there is no way to find this out directly. Reading from an uninitialized variable invokes undefined behavior and there is no way to recover from that. However, you have a few options to prevent this from happening.

If your variables have meaningful default values, just initialize them to those defaults before doing anything else with them. You still won't be able to tell the difference between a variable that was defaulted and one that just happens to have the same value but if your logic works without that knowledge, then this is perfectly fine. (For example, if you were to plot a sine function f(x) = a + b sin(c x + d), it would be natural to default a and d to 0 and b and c to 1.)

If your parameters don't span the entire value range of the data type, you could initialize them to some sentinel value and check for that before using them. For example, you could use NAN for floating-point values or −1 for quantities of any type that cannot be negative. Many people dislike this solution because it is all too easy to forget checking the value and using the sentinel as if it were a proper value, thus computing garbage. I believe that there is a time and place for every technique but this one is probably indeed overused.

If everything else fails, you can add a flag per variable that indicates whether it has a value. To make this usable, I recommend you create a data type with a reasonable name such as this one.

struct optional_float
{
  bool initialized;   // always safe to read, set appropriately
  float value;        // only safe to read if initialized == true
};

In C++, you can do much better by using the new std::experimental::optional<T>. But I realize that the C++ tag was removed from your question.

Finally, be sure to run your code through a debugger such as Valgrind or use a sanitizer to detect accidental reads from uninitialized variables.

Upvotes: 2

Ezio
Ezio

Reputation: 1175

I don't think there is method to check a variable wether initialiez or not . As default variable in c has a random value or 0 before you initialize it.

If you want to know wether a variable is initialzed ,you can make a legal value scope of this variable. Before you use it ,you can check wether the value is inside the scope.

The best method in c to safty using variables is checking it by yourself if you are to use it.

Upvotes: 0

Related Questions