Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
When we debug the programme , even before the memmory allocation takes place , there are some values that are present in the variable.
Where these values come from?
With out memmory allocation , how the variables can contain the values?

What I have tried:

When debugging the program during first step we can observe the values are present in the variables , before memory allocation . How is that possible?
Posted
Updated 9-Jun-21 8:18am
Comments
CPallini 9-Jun-21 7:34am    
You should post the code in order to get help.
(anyway, for instance, in C, uninitialised variables contain garbage values at program start).

There are two types of variables: initialized and uninitialized.
The first set are variables where you have specifically given them a value when your created them:
C
int x = 666;
And the system sets the value each time the variable is created (that may be often: local variables are created when the function is executed, and destroyed when it exits).

The second set is different: if you don't specify a value explicitly, then the system can do one of two things:

1) Give it a default value (usually zero) when it is created.
Or
2) Give it no value, in which case the memory remains at whatever value it was holding when it was last used - which is unlikely to be useful, and may not even be consistent, because memory is "recycled" and may have been used for a totally different variable (or even a return address!) last time before it was allocated to your shiny new variable.

The selection of these is generally controlled by compiler switches, and in most cases they default to "no value set"

You should always ensure that you set a value before you use a variable, as you can't guarantee that it will behave the same way each time it is run!
 
Share this answer
 
The values are just whatever happened to be left there by the previous user of that memory (another program). When that program ended, the memory was just marked as available for use. But it wasn't cleared. Files on a disk work the same way. When a file is deleted, the space that it occupied is just marked as available. The original information is still there until the space is reassigned to a new user who overwrites it.
 
Share this answer
 
v3
Your code gets allocated a piece of memory before even starting, courtesy of the OS and its process launcher.

A segment of this memory is the stack, which holds the local variables for each function (including main) currently running, their parameters, eventually the backups of some registers and the return addresses of the functions.

Both the piece of memory supplied by the OS and the stack are uninitialized, which means that in the best case scenario they contain random values stemming from the half charges stored in each memory cell; otherwise that memory could have been formerly of other processes, which filled it with their own data; finally the stack is never cleaned, it shrinks and expands but the values are never cleared whn it shrinks and are overwritten only when it expands.

That's why you get dirty memory before memory allocation.

Another thing to add: when compiling with debug options some compilers (as VisualStudio) prefill the memory with "magic values" that help identify unitialized variables: in case of stack memory you would have every byte preset to 0xCC or 0xCD, for heap memory 0xFE, deleted heap memory 0xFD, guard aread before and after allocated heap memory 0xAB.
 
Share this answer
 
In C and C++ only the plain variables are allocated and any other work is not done. This is a legacy of the old times when the phisical memory computing power was so limited, that optimizatino for less usage was a primary goal. So the progammer it is responsible for do it, when needed. Most important is this issue when working with pointers and the most common cause of nasty bugs and crashes!!!

Other languages have other conventions but so there arent so fast as C. C is the car without any safety features: mean, lean but fast and running on simple devices like ASICS .:-)
 
Share this answer
 
it is related with C++ default initialization.
please see this link
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900