Can I save multiple values in one memory address? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

4th Nov 2020, 2:11 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
44 Answers
+ 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.
4th Nov 2020, 6:42 PM
Shadow
Shadow - avatar
+ 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.
4th Nov 2020, 2:28 PM
Davide
Davide - avatar
+ 4
Of course, you can >) And how to do it - it's another question)) 😁
6th Nov 2020, 8:27 AM
Кайто Судзуки
Кайто Судзуки - avatar
+ 3
NotAPythonNinja the address should be the same even outside the loop, no?
4th Nov 2020, 2:36 PM
Davide
Davide - avatar
+ 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?
5th Nov 2020, 1:44 PM
Davide
Davide - avatar
+ 3
Martin Taylor I tried it! I saw the overflow from 4 billion to zero! Thank you for the experience!
5th Nov 2020, 9:09 PM
Davide
Davide - avatar
+ 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
4th Nov 2020, 3:29 PM
Davide
Davide - avatar
+ 2
NotAPythonNinja but funky results are not useful 😆
4th Nov 2020, 3:35 PM
Davide
Davide - avatar
+ 2
Hihi, the old man was using assembler. But over 30 years ago 🤔
4th Nov 2020, 3:45 PM
Coding Cat
Coding Cat - avatar
+ 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 🤯
5th Nov 2020, 12:34 AM
Davide
Davide - avatar
+ 2
Martin Taylor Yes! I already know about fixed size of int in memory. I forget about it when I was writing question 😂. Sorry!
5th Nov 2020, 3:07 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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.
5th Nov 2020, 7:37 AM
Amit Roy
Amit Roy - avatar
+ 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.
5th Nov 2020, 8:43 AM
Amit Roy
Amit Roy - avatar
+ 2
You're right. Whatever c is mother.
5th Nov 2020, 6:06 PM
Amit Roy
Amit Roy - avatar
+ 2
Read "Let us c" by yashvant kanetkar.
6th Nov 2020, 5:45 PM
Amit Roy
Amit Roy - avatar
+ 1
Please let me know if I'm wrong. Then I will delete this. But maybe it's possible to store for example two 8 bit values in one 16 bit wide adress. One at high byte and one at low byte and then using some kind of mask?
4th Nov 2020, 3:18 PM
Coding Cat
Coding Cat - avatar
+ 1
NotAPythonNinja Of course they are different processes. I know what I'm questioning about. And Thanks!
4th Nov 2020, 5:08 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
I was unable to find mine answer on Google that's why I'm here.
4th Nov 2020, 5:08 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
Coding CAT May be you're right.
4th Nov 2020, 5:12 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
for multiple you have to use lists and dictionary
5th Nov 2020, 2:53 AM
Deepak Jaiswar
Deepak Jaiswar - avatar