Memory Concepts

Posted by Didi Setyapramana On 3:14 AM 0 komentar

  1. //Fig.1.6: fig 01 06.cpp
  2. //adtion program
  3. #include<iostream>
  4. //function main begin program execution
  5. Int main()
  6. {
  7. Int interger1;
  8. Int interger2;
  9. Int sum;
  10. Std::cout<<”Enter first interger\n”;
  11. Std::cin>>interger1;
  12. Std::cout<<”Enter second interger\n;
  13. Std::cin>>interger2;
  14. Sum= interger1+interger2;//assign result to sum
  15. Std::cout<<”sum is”<<sum<<std::endl;
  16. Return 0;
  17. }


Variable namnes such as interger1,interger2 and sum actually correspond to locations in the
computer memory.
Every variable has a name ,a type,a size and a value.
In the addition program of Fig. 1.6,when the statement

Std::cin>>inerger1;

In the line 13 is executed,the character typed by the user are converted to an interger that is
placed into a memory location to which the name interger1 has been assigned by C++ compiler.
Suppose the user enters the number 45 as the value for interger1.
The computer will place 45 into location interger1 as shown in Fig.1.7
Whenever a value is placed in memory location,the value overwrites the previous value
in that location.
Returning to our addition program,when the statement.
Std::cin>>interger2;

In line 16 is execute,suppose the user enter value 72.this value is placed into location
interger2,and memory appers as in Fig 1.8,Note that these locations are not Necessarily adjacent
in memory.
Once the program has obtain values for interger1 and interger2 ,it adds these
values and places the sum into variable sum .the statement
Sum = interger1+interger2;

That perform the addition also replaces whatever value was stored in sum.This occurs
When the calculated sum of interger1 and interger2 is placed into location sum(whith
Out regard to what value may already be in sum;that value is lost).After sum is calculoated,
Memory appear in fig 1.9.

Note that the value of interger1 and interger2 appear exactly as they did before they were
Used,but not destroyed,as the computer performed the calculation.Thus,when a value is read
Out of a memory location,the process is nondestructive.

Interger1
45

Interger2
72

Sum
117

Fig 1.9 Memory locations after calculating the sum of interger1 and interger2

0 Response for the "Memory Concepts"

Post a Comment