Can I save multiple values in one memory address? | SoloLearn: Learn to code for FREE!

+4

Can I save multiple values in one memory address?

I wrote a program which will run for ever and increment value by 1 of variable and display the value and it's address if have enough memory . I run this program three time and each process was showing same memory address with different values. A video for better understanding: https://ShailMurtaza.000webhostapp.com https://code.sololearn.com/cHefUk5jogYa/?ref=app

11/4/2020 2:11:23 PM

Shail Murtaza

52 Answers

New Answer

+14

The reason the two programs display the same address is because they are displaying a virtual address. Think of this address as a relative address in memory from the start of the program. A program gets compiled with multiple sections. There are code sections, constant sections, predefined data sections, undefined data sections, the stack, and the heap. The linker links all of the different sections of your code together into a program. The addresses to the different sections of this program are now all relative to the start of the executable file. When the progam is loaded into memory all of these addresses, that are relative to the start of the file, get translated by the operating system loader into virtual addresses. Since you are running two copies of the same program the offset address in the file is the same in both instances so the virtual address is the same. That is why the address displayed in both copies of the program is the same. The addresses are not real physical addresses in memory. Each program is showing its own virtual address.

+8

What Shadow said 👆 Sounds to me like you are confused by virtual addressing. The memory addresses that you see are not real addresses but are mapped to real addresses through a complex process of dictionary lookups.

+7

@Davide Yes numbers do overflow and big numbers take some time to do it. It is a definite gotcha in many an embedded system. Clocks that count the system tick do overflow eventually and it needs to be taken into consideration. Interestingly my son showed me a game he was playing that was glitching out. The aircraft was twitching uncontrollably and the airspeed was -2.1 billion kts. Looks like someone messed up a calculation and overflowed a signed integer. The game No Man's Sky uses a 32-bit signed integer in the Milestones achievements page so the maximum number of units displayed in the achievements is 2.1 Billion. However, the units display in the player stats uses an unsigned integer. When you hit 4.2 billion units you can't earn any more units. On a more catastrophic note the crash of the Ariane 5 rocket was due to an integer overflow. http://www-users.math.umn.edu/~arnold//disasters/ariane.html

+5

The video does not load for me, so maybe I misunderstand the problem, but due to virtual adress mapping, it should totally be possible for two processes to access the same memory adress, shouldn't it? Thus it would be possible for two instances of the program to show different values of 'a' while displying the same adress value.

+5

I recall only two books by title and author that I have read: 1) Brian W. Kernighan, Dennis M. Ritchie The C Programming Language Which covers the essentials of ANSI C. 2) Ray Seyfarth Introduction to 64 Bit Intel Assembly Language Programming for Linux Which includes a chapter on memory virtualisation. Here is an article on memory optimisation, I read, which goes into details about how computer memory works and how to structure a program to optimise memory usage and access time (it may go a little deep for your purposes, I can imagine): Ulrich Drepper What every Programmer should know about memory Other than that * I read books on Microcontrollers * have done a lot of reverse engineering * electrical engineering was part of - my education to become a ham radio operator - the computer science curriculum at the university * I studied both physics and mathematics at the university But I do not recall any particular book thereof that I would be certain to be helpful to you. A lot were also just lecture notes.

+4

Shail Murtaza NotAPythonNinja is right. Instead of doing while(1), do while(a<10) and please add a newline \n at the end of the printf. You'll see that the value of a is changing while the address is always the same. However every time you run the program the variable is stored in some place in memory that may or may not be the same place used in previous runs. But again, in each run the address never changes.

+4

No you can't have two different variables accessing the same memory address. The two programs in your video are two different processes running independently. As Coder Kitten pointed out the addresses are not real addresses they are virtual addresses. Since you are running two copies of the same program the relative addresses in both instances are the same virtual address. Each time you increment the variable the address of the variable does not change. So your comment of "if have enough memory" is meaningless. The variable does not expand to use up more memory as it gets larger, it is a fixed size. A signed integer has a maximum value. If you increment it beyond that value a sign inversion occurs and the numeric value flips from the maximum value to the minimum value (largest negative value if you prefer). If you continue to increment the variable it will reach the maximum value then overflow again. Since a 32-bit signed integer can span about +/- 2.1 billion which means to wrap round from zero all the way back to zero is about 4.2 billion iterations. Assuming 1ms average for the screen update that's about 4 million seconds, or about 47 days. Here is a program that demonstrates sign inversion on overflow. Don't worry it doesn't take 47 days to run. I preload the count just below it's maximum value and only make 8 increments. https://code.sololearn.com/cjC7IqzXEXZ7/#cpp

+4

Of course, you can >) And how to do it - it's another question)) 😁

+3

NotAPythonNinja the address should be the same even outside the loop, no?

+3

Martin Taylor that's really interesting! Thank you for sharing! But what happens when you hit 4.2 billion with a 32-bit unsigned int? The bits overflow starting again from 0000000..0000 or what?

+3

@Davide, Yes, if you are using an unsigned 64-bit integer and keep incrementing it it wraps around from the max value to zero. The CPU actually has a carry flag in it's registers which can detect this condition. When assembly language programming I always test this flag to see if the computation is valid. To repeat my code example with an unsigned integer change the variable declarations and use UINT_MAX as the constant. To repeat my code using unsigned long change the variable data types, use ULONG_MAX, and change the bitset to use long. You could use literal values of 32 or 64 for the bitset declarations but that is not portable across systems.

+3

Martin Taylor I tried it! I saw the overflow from 4 billion to zero! Thank you for the experience!

+2

Coding CAT I think that what you said is something like: short int x = 15; short int y = 32; int together = x * 100 + y; // 1532

+2

NotAPythonNinja but funky results are not useful 😆

+2

Hihi, the old man was using assembler. But over 30 years ago 🤔

+2

Martin Taylor wow! I couldn't believe it so I made the math and I found that it takes 49.7 days to display all the possible numbers. But what about 64-bit integers? For them it takes around 6 years and 10 months 😱 I am sorry for being out of thread but that's a shocking fact that need to be told 🤯

+2

Martin Taylor Yes! I already know about fixed size of int in memory. I forget about it when I was writing question 😂. Sorry!

+2

You can if you know bitwise programming. You can even store full date in a char variable which is obviously 1 byte in c. I learnt this from a book named "let us c" by yashvant kanetkar.

+2

Yeh and you can create some functions to divide a 1 byte variable into 2 4 bit or other combinations. it's possible I was into bitwise programming and food that kind of thing.

+2

You're right. Whatever c is mother.