answersLogoWhite

0


Best Answer

Dynamic initialisation of a variable refers to variables that are initialised at runtime rather than at compile time. Consider the following example:

// return the sum of all numbers in the range [0:n]

const unsigned f (const unsigned n) { return n<=1?n:n+f(n-1); } // recursive!

int main (void) {

unsigned x {f (42)}; // dynamic initialisation

// ...

}

Here, x has to be dynamically initialised (at runtime) because the return value of f () cannot be determined at compile time.

The function f() has a linear time complexity (O(n) in big-O notation) however we can improve the execution time by removing all the recursions and reducing the function to a much simpler constant-time calculation:

// return the sum of all numbers in the range [0:n]

const unsigned f (const unsigned n) { return (n+1)*n/2; }

While this will greatly reduce the runtime cost, we still incur dynamic initialisation, albeit in constant time (O(1) as opposed to O(n)). Ideally we want to eliminate dynamic initialisations wherever possible.

Note that in the original call, we passed the constant value 42. Constant expressions such as this are extremely useful because they allow us to perform compile-time computation and thus avoid (some) dynamic initialisation. We can take advantage of this by declaring the function constexpr rather than just const:

// return the sum of all numbers in the range [0:num]

constexpr unsigned f (const unsigned n) { return (n+1)*n/2; }

int main (void) {

unsigned x {f (42)}; // static initialisation

// ...

}

Through compile-time computation, the example is now functionally equivalent to:

int main (void) {

unsigned x {903}; // static initialisation

// ... }

In other words, the function call is eliminated completely so we incur no runtime cost at all. The value 903 is the return value of f (42). Just as importantly, if we subsequently called the function with a variable expression, the compiler will generate a function call which will be invoked at runtime. Thus we get the best of both worlds: compile-time computation when possible and constant-time dynamic initialisation if (and only if) it is needed.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

//variable definition int x; //variable assignment x = 10; //variable definition and assignment int x = 10;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What do you mean by dynamic initialization of a variable Give an example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you give an example of array using character variable in c program?

the example of array over charcter variables is char ["string"]


How do you give values to variable?

it depend on the programming language u use. for instance in c, just decalre the variable and equal it to the value it is to take. eg. my_age=21; /*my_age is the decared variable and 21 is the assigned value*/ //for php $my_age=21; /*my_age is the decared variable and 21 is the assigned value*/ for strings (in php)eg. $my_age="none of ur business" /*$my_age is the variable and the string "none of ur business" is the assigned value*/


What is poiner value in c?

In c a pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer. Pointer declaration A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable. type * variable name Example: int *ptr; float *string;


What type of reference is C19?

A reference variable is used to refer to or give access to an object. A reference variable is declared to be of a particular type and that type can not be altered.


Advantages of having no type in a language?

A type system is generally very useful. It prevents you from making many mistakes. For example adding 1 to "cool" would result in a garbage answer but you might do it accidentally without types. However types are sometimes constraining and inflexible. Perhaps I really do want to add 1 to 'c' because I know exactly what new ascii value it would give me. Type systems also increase compile time, run time, or both. Note: You may be confusing no typing with dynamic typing. With dynamic typing you generally do not label a variable with a type in its declaration. Scripting languages like python are often like this. However they remain strongly typed languages. Assembly is an example of a untyped or one type language.

Related questions

Define a variable and give an example?

int x; "Example"


Give you an example of independent variable?

time


Give an example of a quantitative variable?

Shoe SizeHeightWeightAge


What is declaring pointer variable?

I'll give you an example: char *s;


What are the independent and depent variables?

You can tell which is the independent variable and which is the dependent variable by changing the equation into an "if/then" statement. Example: y = 3x In this example, x is the independent variable and y is the dependent variable. If you give me x, I will tell you y. If x = 1, then y = 3 If x = 2, then y = 6 So you give me the independent variable, and then I will be able to determine the dependent variable.


Can you give an example of variable in a science project?

What is changed, either by you or the different results. The mother-category of the IV and DV. (independent VARIABLE, and dependent VARIABLE) :)


Can you give an example of different kinds of variable?

use loaves in a meaningful sentence


Can you give an example of something outside of math that we might call variable?

weather


Can you give an example of the word dynamic in a sentence?

In music, the strength of the sound (loud or soft) is called the dynamics.


Give five example of constant and variable?

pi, e, phi, 1,2,3,4, etc.


Can you give an example of array using character variable in c program?

the example of array over charcter variables is char ["string"]


What is a dependent or an independent variable?

An independent variable is something that you can change in your experiment. A dependent variable is something that changes depending on what your independent variable is. Example: You have two plants. You water one every day and you water the other one every other day to see how fast they'll grow. Your independent variable is water, because you can change how much you give to the plant. Your dependent variable are the plants, because they'll change depending on how much water you give them.