Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
7646 Discussions

How to set a specific value to uninitialized variables in intel C compiler

BH_Kim
Beginner
677 Views

I am testing with code that cannot check the previous execution environment.

And in optimization flag O2, I found that uninitialized variables are initialized to inconsistent values each time I try to run.


Is there a way to get the Intel compiler to initialize uninitialized variables to a specific value?
(Any compiler options?)

And how does the value of uninitialized variables change depending on the setting of the optimization flags(O0 or O2) in the Intel compiler?

Thanks.

0 Kudos
7 Replies
Gopika_Intel
Moderator
627 Views

Hi

Thank you for reaching out.

>Is there a way to get the Intel compiler to initialize uninitialized variables to a specific value?

There are various ways to catch hold of uninitialized variables in the code. Some of them are listed below:

  • By specifying keyword uninit for option check.

Ex: icpc -check=uninit test.cpp

  • By using -ftrapuv(/Qftrapuv in case of windows) which initializes stack local variables to an unusual value to aid error detection

Ex: icpc -ftrapuv test.cpp

  • In Windows, By using /0d /RTC1 which initializes all local variables to a non-zero value.

>how does the value of uninitialized variables change depending on the setting of the optimization flags(O0 or O2) in the Intel compile?

We will discuss this with the internal team and get back to you

Thanks and Regards

Gopika


BH_Kim
Beginner
572 Views

I work with Linux, so using '-check=uninit' would be very helpful.
Thank you and I'll look forward to your response to my second question.

Viet_H_Intel
Moderator
622 Views

You can also use -Wuninitialized flag to get a warning if a variable is used before being initialized.

C/C++ does not automatically initialize variables to a given value. So when the compiler assigns a memory location to a variable,

the default value of that variable is whatever value happens to be in that memory location, often is a garbage.


Thanks,



BH_Kim
Beginner
570 Views

Viet_H_Intel - After your explanation, I got a better understanding of the variables initialization in C compiler.
Thank you for your answer.

Viet_H_Intel
Moderator
489 Views

>how does the value of uninitialized variables change depending on the setting of the optimization flags(O0 or O2) in the Intel compile?

Generated code between O0 vs.O2 will be different, so memory assigns for that variable won't be the same location between to cases, so its value is just whatever happens to be in that location.


Viet_H_Intel
Moderator
424 Views

Let us know if this is still an issue. Otherwise, we will close it.


Thanks,


Viet_H_Intel
Moderator
405 Views

We will no longer respond to this thread.  

If you require additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only.

Thank,


Reply