Can I access anything in memory?
c, memory-management, pointers
Solution
Oh my, well.
Technically, yes, you can. In embedded devices which don't use a MMU or any form of protection (or, you know, x86 in real mode), you can do exactly what you have posted there. You can also do it in user mode on any operating system, but the chances of you actually hitting valid memory are very small.
In reality, no, you can't just do that. Given virtual memory and memory protection, it's very likely that the region you try to access will not have been mapped and hence, will fail. In addition, if you hit protected memory (e.g. anything belonging to the OS), your access will fail. Both of these scenarios are what cause segmentation faults.
Your statement is valid (for varying definitions of valid), and the program will try to access the memory you requested. It's just that it might not actually be mapped to anything.
It's worth also noting that this is how memory mapped I/O works. Say I have a hardware control register which when written to, writes a byte over the attached UART/serial line (and, for simplicity, it works as magic and doesn't need any other registers to be set). In C, this would be written as follows for my overly simplified device:
#define UART1_OUT 0xFC56
volatile char* uart = UART1_OUT; // Definition of pointer to variable.
// volatile is required here. Look it up, but
// it basically stops your compiler optimising
// anything to do with this variable
*uart = 'A'; // Write an A character to the serial line
Of course, real-world devices are a little more complex ;).
Problem
Disclaimer: I am new to C/C++. If I were to manually assign a memory address to a variable in C, and then try to echo out that value...do I have unrestricted access to view anything in memory or are there restrictions in place? For example: ``` char * p = (char *)0x28ff44; printf("Memory value: %c", *p); ``` I'm guessing that would crash horribly if what was at that address didn't fit the size of char type, but it's merely an example. What I really am curious about is if something like this is possible or do operating systems impose restrictions and only allow you to access memory in your given memory space?