answersLogoWhite

0


Best Answer

The value of an uninitialised variable will be whatever value happens to reside in the allocated memory when that variable is instantiated. Attempting to access that value will result in undefined behaviour.

There advantages to uninitialised variables. For instance, when allocating a large amount of memory prior to a memory copy, there's no point in initialising the memory since it will be initialised by the copy operation.

In object-oriented programming, uninitialised variables do not exist. All variables are initialised at the point of instantiation via the object's class constructor. However, if the language also supports primitive data types (as C++ does), then those types will generally be uninitialised since primitives have no constructors.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What value is stored in an uninitialised variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Do variables that are declared but not initialised contain garbage values?

It depends on the language. In C and C++, all static variables are zero-initialised at runtime but local variables are not, thus an uninitialised local variable will hold whatever value happens to reside at the memory allocated to the variable at runtime. However, the C/C++ compiler can be configured to warn against using an uninitialised variable. In object oriented languages like Java there's no such thing as an uninitialised variable. This is because all Java variables are objects so we must pass the initial value to the object's constructor unless the object has default constructor. Attempting to default construct an object that has no default constructor is a syntax error.


Where does global variables stored in C?

It depends entirely on what platform you are using. In an embedded environment, for instance global/static variables go into different RAM memory segments depending on whether or not they are initialised. constants are often left in ROM automatic variables are normally placed of the stack of the currently running task but not always.


What is BSS segment in C?

The BSS segment is where all uninitialised static variables are allocated at runtime. BSS is an acronym for Block Started by Symbol, however it can also be referred to as the BES segment (Block Ended by Symbol) depending on how it is physically implemented. Regardless of how it is implemented, we can simply refer to it as the uninitialised data segment. Unlike the data segment which is initialised directly from a bitmap image stored within the object file itself, the uninitialised data segment is dynamically instantiated at load time as a contiguous block of zero-initialised memory. Only the length of the block need be stored in the object file. Uninitialised static variables are simply those that have not been explicitly initialised. Static variables may be declared globally (at file scope, outside of any function) or locally (inside a function): int x; // uninitialised global variable void foo () { static int y; // uninitialised local variable } Both x and y will be allocated in the uninitialised data segment. Although all constant variables must be initialised at the point of declaration in C, a constant variable that is initialised with the value zero may be allocated in the uninitialised data segment rather than in the initialised data segment where all other initialised static variables and constant variables are allocated. Similarly with static variables initialised to zero. This helps reduce the size of the object file and thus improves load time. In memory, the text segment (.text) contains the executable code and is loaded first, followed by the data segment (.data) and finally the BSS segment (.bss). The object file header allows the program loader to allocate memory to all three segments as a single contiguous allocation. The .text and .data segments are initialised from bitmap images stored within the object file itself while the .bss segment is typically zero-filled-on-demand. However, in embedded systems software, the C runtime must initialise the BSS segment prior to entering main().


How do you find the smallest value in a set of numbers in qbasic?

Store the numbers in a suitable container such as an array. Assume the first number is the smallest and assign its value to a local variable. Traverse the remainder of the sequence, comparing each element's value to the stored value. If an element has a lower value, assign its value to the local variable. When the sequence is fully traversed, the local variable will hold the value of the smallest value in the sequence. Return that value.


Is it true that passing data by reference enables you to change the value stored in a variable in one file using a function in another file?

Yes, passing a variable by reference gives you a pointer to the original variable, meaning you can change its value from within the function being called and the change will affect the original variable.

Related questions

How occur garbage value?

When you use an uninitialised variable, for example.


When posting variables if you do not value the variable then you want to display the variable value. What is going on Explain?

I believe you are referring to uninitialised variables, although it's hard to tell from the wording of the question. If you attempt to print the value of an uninitialised variable, the behaviour is undefined. Typically, when a variable is uninitialised, the memory to which the variable is allocated will be left unchanged; it will hold whatever value happened to exist there at the point the variable was instantiated. However, only local variables (allocated on the stack) and dynamic variables (allocated on the heap) are uninitialised by default. Static variables are zero-initialised by default and object variables are always initialised according to whichever class constructor was invoked at the point of instantiation. Most compilers will detect attempts to use uninitialised local variables and will emit warnings to that effect during compilation.


What is a NULL Pointer Whether it is same as an uninitialized pointerin C language?

A null pointer is a pointer that holds the value zero (0), which simply means it does not point at anything in particular. It is an error to try and dereference memory address 0x0, as this address is reserved by the system, and will result in undefined behaviour. Pointers must be tested to ensure they hold a non-null address before being dereferenced. An uninitialised pointer is the same as any other uninitialised variable; no value has yet been assigned to it. The value of an uninitialised variable will be whatever value happens to reside in the memory allocated to that variable. All variables, including pointer variables, must be initialised before being accessed for the first time and most compilers will warn against accessing an uninitialised value. Dereferencing an uninitialised pointer has undefined behaviour.


What is a function of a variable?

A variable is a named memory address in which a value may be stored and mutated.


Do variables that are declared but not initialised contain garbage values?

It depends on the language. In C and C++, all static variables are zero-initialised at runtime but local variables are not, thus an uninitialised local variable will hold whatever value happens to reside at the memory allocated to the variable at runtime. However, the C/C++ compiler can be configured to warn against using an uninitialised variable. In object oriented languages like Java there's no such thing as an uninitialised variable. This is because all Java variables are objects so we must pass the initial value to the object's constructor unless the object has default constructor. Attempting to default construct an object that has no default constructor is a syntax error.


What is uninitialised pointer?

A pointer is an address or the name for the location for an item of data. An uninitialised pointer is one that has not been assigned an initial value or item of data.


What is a C variable?

A variable is an entity that may change its value. In a program, the result of the processing statements are stored in the computer's memory.


What is 'get' in java?

when you use "get" you are usually getting a value that's stored within variable. Alternatively , use "set" to set a value


What is BSS segment in C?

The BSS segment is where all uninitialised static variables are allocated at runtime. BSS is an acronym for Block Started by Symbol, however it can also be referred to as the BES segment (Block Ended by Symbol) depending on how it is physically implemented. Regardless of how it is implemented, we can simply refer to it as the uninitialised data segment. Unlike the data segment which is initialised directly from a bitmap image stored within the object file itself, the uninitialised data segment is dynamically instantiated at load time as a contiguous block of zero-initialised memory. Only the length of the block need be stored in the object file. Uninitialised static variables are simply those that have not been explicitly initialised. Static variables may be declared globally (at file scope, outside of any function) or locally (inside a function): int x; // uninitialised global variable void foo () { static int y; // uninitialised local variable } Both x and y will be allocated in the uninitialised data segment. Although all constant variables must be initialised at the point of declaration in C, a constant variable that is initialised with the value zero may be allocated in the uninitialised data segment rather than in the initialised data segment where all other initialised static variables and constant variables are allocated. Similarly with static variables initialised to zero. This helps reduce the size of the object file and thus improves load time. In memory, the text segment (.text) contains the executable code and is loaded first, followed by the data segment (.data) and finally the BSS segment (.bss). The object file header allows the program loader to allocate memory to all three segments as a single contiguous allocation. The .text and .data segments are initialised from bitmap images stored within the object file itself while the .bss segment is typically zero-filled-on-demand. However, in embedded systems software, the C runtime must initialise the BSS segment prior to entering main().


Where does global variables stored in C?

It depends entirely on what platform you are using. In an embedded environment, for instance global/static variables go into different RAM memory segments depending on whether or not they are initialised. constants are often left in ROM automatic variables are normally placed of the stack of the currently running task but not always.


How are we going to use pointer in a program?

A pointer is a variable used specifically to store a memory address. We say the variable "points to" the memory address because we can dereference the pointer to access the value stored at that address. The pointer's type determines how that dereferenced value will be interpreted. Being a variable, we can change the stored address and thus change which value we point at. This makes it possible for the same variable to refer to different objects in memory, which includes other pointer variables.


How do you find the smallest value in a set of numbers in qbasic?

Store the numbers in a suitable container such as an array. Assume the first number is the smallest and assign its value to a local variable. Traverse the remainder of the sequence, comparing each element's value to the stored value. If an element has a lower value, assign its value to the local variable. When the sequence is fully traversed, the local variable will hold the value of the smallest value in the sequence. Return that value.