0

Could we point and address specific place in memory using pointer in c language? Then modify it from another file (ANOTHER PROGRAM) and view it from any where. Like :

Modifying it :

#include<stdio.h>

void main(){


   int *p;
   p= 12345678;
   scanf("%d",p);

}

Viewing it :

#include<stdio.h>

void main(){


   int *p;
   p= 12345678;
   printf("%d", *p);

}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 2
    Not with modern operating systems. The OS virtualizes and protects memory for each process and you can't reach across processes like that. – edtheprogrammerguy Feb 16 '21 at 20:38
  • 1
    To access the same memory you would need to use OS specific shared memory functions. But then you don't specify the address directly, you give the shared memory a name and open and access it through the name. – Werner Henze Feb 16 '21 at 20:41
  • Thanks very much, That's exactly what i am trying to figure out! @edtheprogrammerguy – mohannadalnono Feb 16 '21 at 23:13
  • Could you give examples for OS that can freely access the memory? Thank you! @WernerHenze – mohannadalnono Feb 16 '21 at 23:15
  • @mohannadalnono For example https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c and https://stackoverflow.com/questions/1200998/sharing-memory-between-two-processes-c-windows – Werner Henze Feb 17 '21 at 08:02
  • I understand the memory-map. What i am asking is : i want to make pointer that points to address that i enter manually, not to let the OS choose the address that is available. PS: it seems to be dangerous but i will not modify data that was exist in that address, i will just read it. – mohannadalnono Feb 17 '21 at 15:19

2 Answers2

1

No. Each process in your operating system has its own address space.
Processes can communicate, using the channels provided by the operating system.
Look into IPC, aka inter process communication, if you're interested in knowing more about it.

Jack Lilhammers
  • 1,207
  • 7
  • 19
  • Yeah that's what i thought, thanks. Yes i would love to study it more, is there preferred references for beginners? – mohannadalnono Feb 16 '21 at 20:43
  • I don't really remember if we had a specific textbook at the university, but I'll look into it now – Jack Lilhammers Feb 16 '21 at 20:48
  • 1
    I sorry, but I can't recommend any particular text. However for the basics you'll be fine reading articles online, then too go deeper you could search the computer science and computer engineering courses an their slides – Jack Lilhammers Feb 16 '21 at 20:57
0

What about const volatile pointer (which will be pointing to some memory)? If I'm not wrong it will point to some memory it won't change during first program (also you will not be allowed to modify this memory) but that memory can be modified by another program/process etc.

EDIT:

Just to clarify if we use const volatile pointer it will do two things.

  1. In program it won't allow us to change it value after initialisation
  2. More important volatile keyword will disallow some compiler optimisations on readings and will always reload from memory each time it is accessed by the program. This dramatically reduces the possible optimisations. However, when the state of an object can change unexpectedly, it is the only way to ensure predictable program performance.
  • Actually, i was trying to read data on specific addresses that i determine manually. – mohannadalnono Feb 16 '21 at 23:21
  • 1
    So that it could work anyway but only if you will write program for arduino or other development board or embeded system. But generaly speaking for system programing is like @Jack Lilhammers told – Patryk Gawroński Feb 16 '21 at 23:41
  • Yeah, that's really interesting, So the modern OS like windows prevent us to access the memory freely. But i am thinking if we knew the technology that do that ( preventing us to access specific address ), so i we can cancel/pause that technology in some way or another. Is that's possible? – mohannadalnono Feb 17 '21 at 15:08
  • 1
    As you told it will prevent us but only if we hadn't obtain administrative rights :) Let's skip that "messing with other process memory is a bad thing" - you want to learn. So on operating systems such like windows or linux distro we can read and modify other process memory after obtaining proper rights otherwise system will block our attempt. On windows you just use OpenProcess, ReadProcessMemory, WriteProcessMemory functions to play with other program memory. On linux you need to open file from "/proc//mem" and read and write like normal file. – Patryk Gawroński Feb 18 '21 at 23:09
  • 1
    I think I can record something to show you what I had in mind and what to do step by step how to play with other process memory. Of course if you want. – Patryk Gawroński Feb 18 '21 at 23:11
  • Yes, I would be happy to learn that. I appreciate it if you've helped me. – mohannadalnono Feb 20 '21 at 12:41