I'm new on world of computing and computers ..I'm developing in C programming and really every time I get stuck in my thinking how really PC delete the old value if we modified it by new value, I mean: lets assume I write on C like this
int i=6;
i=7;
then the value of i is 7, I know why because we have modified the old value by 7, but how does really the PC delete the previous value? I could believe just in "the previous value overwritten and it will be deleted" but I'm curious to know how really it's deleted by assigning modified value?
Is it like magic? still weird for me .. a good explanation would be really appreciated to convince me how PC use overwritten mode operation to delete previous values once we modifying on the same memory's address ..
Computers have a finite amount of storage. They overwrite that storage. It's not like a SSA model where every assignment creates a new variable. Computers really do change the value of an existing storage location.
The C language is defined in terms of every variable having its own memory location. Assigning a new value does not change the address. It's not a reference to a new immutable value, it's really mutating the storage for int i
. Writing a new value implicitly destroys the previous contents of a variable. It doesn't go anywhere.
You can look at an example of using pointers to see that after int *p = &i;
the value you get from *p
changes after you do i=7;
Or google up a tutorial on C pointers for more detail.
It's not "magic", just electrically how a memory cell naturally works. e.g. a single flip-flop can be changed from one state to another by applying an input signal along with a reset signal that tells it to forget its old value.
A physical analogy is a piece of paper with a pencil and eraser, not a big stack of fresh index cards. The "consumable" (pencil lead and eraser) is just electrical power. (Or more specifically, electric charge in CMOS logic. Some other kinds of digital logic require current to flow continuously, using a lot of power.)
CPU registers are typically made of SRAM (static RAM), see https://en.wikipedia.org/wiki/Static_random-access_memory#Writing. One cell is usually made of 6 transistors, for example, like a flip-flop but fancier and with no current flowing except when a new value is set. This stores 1 bit, and can be written with a new value any number of times, by applying a write-enable signal to update the SRAM cell to the value in its input line.
Actual main-memory is typically made of DRAM, where bits are stored as charge on a capacitor. Every time you read it, you have to refresh this charge, but it's still the same concept of a fixed location that can be written with new values.