0

I'm trying to recreate a memory dumper in C. I would like to iterate over memory using pointers and load the value when accessible.

#include<stdlib.h>
#include<stdio.h>

int main()
{
    unsigned long ptr = sizeof(long);
    printf("%s\n", (char*)ptr); // gives memory violation error
    return 0;
}

When I hit printf("%s\n", (char*)ptr) it raises a memory violation exception. Is there any way either to handle this exception or to know if a pointer is accessible? Thanks.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
midugh
  • 608
  • 5
  • 21
  • Very vague, unclear what are you trying to do. Are you after process memory? Physical memory of the machine? Code, data? Let me ask, what exactly are you hoping to find in that memory dump? – Seva Alekseyev Feb 18 '20 at 17:36
  • You can create a signal handle for `SIGSEGV`. But once the error happens, there isn't really any way to recover. – Barmar Feb 18 '20 at 17:37
  • I ´m just trying to know which part of my memory I can access and which infos are loaded either in the program reserved memory and in accessible system memory(if possible). I ´m trying to get the values of the physical memory i can access from my program. – midugh Feb 18 '20 at 17:39
  • 1
    On Linux: Open /proc/self/maps and read it. On Windows: call VirtualQuery. – user253751 Feb 18 '20 at 18:05

0 Answers0