Reputation: 1296
Where pointer points, for instance in C language? (I think it is same in all the languages where addressing is possible) I mean it in a way to which type of memory exactly. (RAM, registers, etc.)
And which of memories are possible to point nowadays?
Upvotes: 1
Views: 1066
Reputation: 62045
All main memory (RAM) can be thought of as nothing but an array of bytes.
And a pointer can be thought of as nothing but an index into this array.
On modern systems with memory virtualization not all memory may be accessed, so if you make a pointer point to any random location and attempt to read or write from that location you are bound to get an illegal memory access exception. However, for as long as you keep your pointers to point to variables of your program, you are fine.
Furthermore, there exists memory which is readable but not writable, either due to permissions set by the operating system, or because it is ROM which has been mapped into the address space of RAM. So, just because you can read a value from a memory location pointed by a pointer, it does not mean you can write to it.
And no, a pointer cannot point to a CPU register. CPU registers are entirely outside of that big array of bytes called RAM. (However, there exist architectures that map certain special I/O registers of hardware into RAM locations, so these registers can in fact be accessed via a pointer. So, it depends on what kind of register we are talking about.)
A lot of confusion about pointers is caused by the fact that when we print their values we see hard-to-understand values like 0x51a4851c
, and yet we never assign literal values to them, like char* p = 0x51a4851c;
.
That's because our programs (and the data that our programs use) are located in seemingly random memory locations like 0x51a48000
, and for many decades now the trend on all but the tiniest embedded systems has been to not have to know (never have to worry about) precisely on which address your program has been loaded into, because that's too much information and of no practical use.
So, when you say char* p = &c;
the compiler will generate code that loads the address of c
into p
without you having to know precisely where c
is located.
And in fact the operating system is free to load your program into whatever address it sees fit, so this address may change between runs of your program, so it is a very good thing that you do not have to know any fixed addresses, because there really aren't any. (Except in the tiniest of embedded systems.)
Upvotes: 4
Reputation: 726809
This depends on computer architecture, specifically, on what is addressable for a CPU.
In architectures with RAM, ROM, and memory-mapped hardware registers, a pointer can point to a location in RAM, in ROM, or to a hardware register.
This gets more complicated on computers with virtual memory management abilities, because the place the pointer points to depends on the context in which the program executes.
As far as C programming is concerned, pointers to objects allocated with malloc
and pointers to objects in automatic memory (stack) will point to a location in RAM. Pointers to functions and pointers to statically allocated constant objects and string literals could point to a location in ROM.
Upvotes: 2
Reputation: 4515
Pointers can point to any addressable memory. This means they cannot point to registers. The type of memory is anything that is memory-mapped, so this could be RAM or ROM for example, or even a file etc. Basically, anything that can be identified my an address.
As Oliver Charlesworth says, a pointer points to an "object". In C(++), an object is just a region of data storage that represents a value.
Upvotes: 3