Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are pointers stored as integers? [closed]

Are pointers in C/C++ (or any language for that matter), integers? A pointer holds a memory address, from 0 to the upper limit of your memory. So in math terms, a pointer could be considered a non-negative integer.

How are pointers stored in C/C++? And in other popular languages?

like image 234
Zzz Avatar asked Jun 14 '13 17:06

Zzz


People also ask

Can pointers store integers?

An integer pointer (like addressOfDigit ) can only store the address of variables of integer type. int variable1; int variable2; char variable3; int *addressOfVariables; * – A pointer variable is a special variable in the sense that it is used to store an address of another variable.

What do pointers store as values?

1.1 Pointer Variables (or Pointers) A pointer variable (or pointer in short) is basically the same as the other variables, which can store a piece of data. Unlike normal variable which stores a value (such as an int, a double, a char), a pointer stores a memory address.

Where the pointer is stored?

ptr is stored at stack.

What is stored in a pointer C?

The Pointer in C, is a variable that stores address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time.


2 Answers

There's a couple of implicit questions here, so let's take each one that I understand is present.

First, are pointers in C/C++ just integeres with a disguise?

Yes, they are, and no, they're not. First, implementation-wise, the most common way to implement points is to just pick the right size of integer for the platform (32-bit or 64-bit) and use that as an absolute address value into addressable memory space.

In this context, you could say that pointers are just integers.

However, pointers in C/C++ has always been defined as black box data types. You're not supposed to use them as integers since they could very well not have what you would call a "numerical value" context that makes sense.

Certainly they look numerical, you can add integers to them to move past them and so on, but all of that is implementation specific, undocumented (on purpose), and thus not really reliable. What I mean by "reliable" here is that if you interpret the pointer as an integer, you should not rely on there being any meaningful relationship between the pointer and the integer.

For a specific platform, compiler, runtime, etc. you might find evidence that it doesn't have any special meaning beyond being treated as a numerical value that addresses a particular byte in addressable space, but I would still refrain from thinking of it that way. A pointer is a black box.

So the next question is if you can reinterpret the memory that holds a pointer as an integer. Well, yes, you can certainly do that, but again, you should consider the actual numerical value of that integer as meaningless. If you have a 32-bit pointer, you can store it in a 32-bit integer, and vice versa, but what that numerical value of the integer means, that I would not place any value on.

A particular implementation could very well take this:

p++;

to mean something like this:

i -= sizeof(element);

ie. reversed memory space. I don't know of any such environment, computer, runtime, compiler, or whatnot that does this, but the documentation allows for it.

And that's what you should be concerned with.

like image 108
Lasse V. Karlsen Avatar answered Sep 29 '22 19:09

Lasse V. Karlsen


From a mathematical point of view, you can argue that a pointer is simply the index into the memory. However, a pointer is not an int, because they can have different sizes. A pointer is stored in the way the hardware dictates it. On some machines it can be an int, but you shouldn't rely on this.

Specifically related to programming languages, they have to take into account the object size they are pointing to, so the arithmetics is different when looking at the absolute values. In this regard it is different from a simple int

like image 32
Devolus Avatar answered Sep 29 '22 19:09

Devolus