initial values of different-type data

15 views
Skip to first unread message

Chenyi LUO

unread,
Dec 9, 2020, 10:43:34 AM12/9/20
to dea...@googlegroups.com
Hi dealii developers,

I would like to ask(confirm)whether zero is the initial value of 
different-type data, e.g. for  double, int, Vector and Tensor. For 
example, if I just define double x;. Does x have an initial value of 0.0?

Moreover, in my task, I set initial values of a variable, e.g. "a",  to 
be one in the whole domain. When I assemble the residual vector, I need 
to first compute the value of "a" at quadrature points (which turns out 
not exactly 1.0). Consequently, when I compute, e.g. "3*a^4-2*a^3+a^2", 
the answer is not zero. Is it avoidable?

Best,

Chenyi

Matthias Maier

unread,
Dec 9, 2020, 12:02:24 PM12/9/20
to dea...@googlegroups.com
You have to be a bit careful - unfortunately, C++ has one of the most
convoluted rules when objects are zero initialized or not.

As a rule of thumb:

* whenever you are working with a plain "double", "float", "int",
etc. it is best to explicitly initialize the variable (because C++ in
most cases does not initialize the variable for you):

unsigned int i = 0;
double d = 0.;
float f = 0.;

* in case of a "container" like std::vector, dealii::Vector,
dealii::Tensor elements are default initialized:

dealii::Tensor<1, dim> t; // zeroes elements
dealii::Vector<double> V;
V.reinit(10); // zeroes elements

std::vector<double> v;
v.resize(10); // zeroes elements

I do not understand your second question :-(

Best,
Matthias
Reply all
Reply to author
Forward
0 new messages