0

I was trying to create a small program to read the contents of the ram that I input. Here is the code:

#include <stdio.h>
#include <unistd.h>

typedef unsigned char BYTE;

int main(){
    BYTE *ptr=NULL;
    BYTE *lastPtr=NULL;
    char bits[8];
    int a=0;
    printf("Memory Adress:");
    scanf("%p",&ptr);
    printf("%p: %c\n",ptr,*ptr);
    printf("Number of bytes to be analysed:");
    scanf("%d",&a);
    lastPtr=ptr+a;
    
    while(ptr<lastPtr){
        for(int i=0;i<8;i++){
            bits[i]=((*ptr) & (1<<i) )!=0;
        }
        printf("%s\n",bits);
        ptr++;
    }
}
  • What valid memory does `ptr` point to when you call `scanf("%p",&ptr);`?? How big is a pointer on your system? Is `%p` valid? A few links that provide basic discussions of pointers may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) – David C. Rankin May 18 '22 at 17:32
  • 4
    You can't input pointers. On a modern protected system with virtual memory (basically every Windows, Linux or macOS system in existence) pointers are local inside processes. No other process will have the exact same memory map as any other process. – Some programmer dude May 18 '22 at 17:32
  • 1
    And if you do input a pointer (which is *technically* possible), the resulting pointer value is unlikely to be a valid pointer to an object that your program can access. Memory *layout* is one aspect of virtual memory systems, but closely tied to that is memory *protection*: you don't have direct access to physical memory, and you have access to only those virtual memory addresses that the operating system grants to the program (which may be laid out differently as frequently as on a per-run basis). – John Bollinger May 18 '22 at 17:47

0 Answers0