+ 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
44 ответов
+ 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.
+ 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
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
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.
+ 2
Read "Let us c" by yashvant kanetkar.
+ 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?
+ 1
NotAPythonNinja Of course they are different processes. I know what I'm questioning about.
And Thanks!
+ 1
I was unable to find mine answer on Google that's why I'm here.
+ 1
Coding CAT May be you're right.
+ 1
for multiple you have to use lists and dictionary