Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To what extent is it acceptable to think of C++ pointers as memory addresses?

When you learn C++, or at least when I learned it through C++ Primer, pointers were termed the "memory addresses" of the elements they point to. I'm wondering to what extent this is true.

For example, do two elements *p1 and *p2 have the property p2 = p1 + 1 or p1 = p2 + 1 if and only if they are adjacent in physical memory?

like image 899
user5648283 Avatar asked Dec 29 '15 14:12

user5648283


People also ask

Are pointers memory addresses?

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items.

How pointer is useful in memory optimization?

Optimization of our code and improving the time complexity of one algorithm. Using pointers helps reduce the time needed by an algorithm to copy data from one place to another. Since it used the memory locations directly, any change made to the value will be reflected at all the locations.

Is a pointer the same as an address in C?

A pointer is a variable that HOLDS memory address, not the address itself. However, you can dereference a pointer - and get access to the memory location. That's it. It's that simple.

How we can access memory using pointers in C?

int *p = malloc(sizeof(int)); Here, we have allocated enough memory to store a single integer and returned the address of that space to be assigned to the pointer p . Now, the only way to work with that space is through the pointer.

How do pointers differ from simple memory addresses in C/C++?

One other way in which a C or C++ pointer differs from a simple memory address due to the different pointer types I haven't seen in the other answers (altrhough given their total size, I may have overlooked it). But it is probably the most important one, because even experienced C/C++ programmers can trip over it:

How many bytes of memory does a pointer consume?

The pointer p also consumes 4 bytes (on most machines in use today, a pointer consumes 4 bytes of memory. Memory addresses are 32-bits long on most CPUs today, although there is a increasing trend toward 64-bit addressing). The location of i has a specific address, in this case 248,440. The pointer p holds that address once you say p = &i;.

Is there a real address in a C pointer?

How exactly that is achieved and whether there is a real address or not, is not defined. A pointer could be just an ID/handle, not a real address. A C pointer is very similar to a memory address but with machine-dependent details abstracted away, as well as some features not found in the lower level instruction set.

What is the point of a pointer?

A pointer is just another variable which is used to hold the address of a memory location (usually the memory address of another variable). So, the pointee is actually a memory address? You disagree with the author? Just trying to understand. The primary function of the pointer is to point to something.


2 Answers

You should think of pointers as being addresses of virtual memory: modern consumer operating systems and runtime environments place at least one layer of abstraction between physical memory and what you see as a pointer value.

As for your final statement, you cannot make that assumption, even in a virtual memory address space. Pointer arithmetic is only valid within blocks of contiguous memory such as arrays. And whilst it is permissible (in both C and C++) to assign a pointer to one point past an array (or scalar), the behaviour on deferencing such a pointer is undefined. Hypothesising about adjacency in physical memory in the context of C and C++ is pointless.

like image 194
Bathsheba Avatar answered Oct 06 '22 07:10

Bathsheba


Not at all.

C++ is an abstraction over the code that your computer will perform. We see this abstraction leak in a few places (class member references requiring storage, for example) but in general you will be better off if you code to the abstraction and nothing else.

Pointers are pointers. They point to things. Will they be implemented as memory addresses in reality? Maybe. They could also be optimised out, or (in the case of e.g. pointers-to-members) they could be somewhat more complex than a simple numeric address.

When you start thinking of pointers as integers that map to addresses in memory, you begin to forget for example that it's undefined to hold a pointer to an object that doesn't exist (you can't just increment and decrement a pointer willy nilly to any memory address you like).

like image 34
Lightness Races in Orbit Avatar answered Oct 06 '22 08:10

Lightness Races in Orbit