3

Since addresses are numbers and can be assigned to a pointer variable, can I assign any integer value to a pointer variable directly, like this:

int *pPtr = 60000;
Dale K
  • 25,246
  • 15
  • 42
  • 71
Kristina
  • 261
  • 2
  • 13
  • 2
    You can do this, yes. That doesn't mean that it's meaningful, though. – Oliver Charlesworth Aug 18 '17 at 23:14
  • you can but not in your way int *p = 10 ; – EsmaeelE Aug 19 '17 at 00:29
  • int *p; *p = &10 ; may work for you. but consider possible that memory location maybe not accessible. – EsmaeelE Aug 19 '17 at 00:31
  • @EsmaeelE-- `*p = &10;` will not work since you can't take the address of an integer constant in C. Even if you could, an `int` may not be able to hold the value. Finally, `int *p = 10;` declares a pointer to `int` and stores the value `10` in that variable. `int *p; *p = &10;` attempts to use the pointer `p` uninitialized, leading to undefined behavior. – ad absurdum Aug 19 '17 at 00:48
  • 1
    You'll need a cast - `int *iPtr = (int *) 60000;`. – John Bode Aug 19 '17 at 02:35

4 Answers4

10

You can, but unless you're developing for an embedded device with known memory addresses with a compiler that explicitly allows it, attempting to dereference such a pointer will invoke undefined behavior.

You should only assign the address of a variable or the result of a memory allocation function such as malloc, or NULL.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Actually, IIRC even just *creating* a pointer that points to an invalid address is UB according to the standard (although the platforms where this actually causes problems are exceedingly rare). – Matteo Italia Aug 18 '17 at 23:36
  • @MatteoItalia Ahhh.. interesting, so on such a system, all pointers would need to be initialized with a valid address upon creation? Yes? Would 'int ptr=&ptr' work for that? I ask because of https://stackoverflow.com/q/45769003/758133 – Martin James Aug 19 '17 at 09:39
  • @MartinJames: I suspect that leaving a pointer not initialized wouldn't be a problem (and that the C standard requires it to work); the core of the issue is that such machines validate pointers *when they are loaded in pointer registers*, so if you load/calculate an invalid pointer they trap; if you just leave a pointer uninitialized, as long as you don't look at it you are fine (the only thing you can do with it is overwriting it content with NULL or a valid pointer). If you are interested, you may have a look at https://stackoverflow.com/q/3838855/ and https://stackoverflow.com/q/1866461/. – Matteo Italia Aug 19 '17 at 17:03
1

Yes you can. You should only assign the address of a variable or the result of a memory allocation function such as malloc, or NULL.

lili
  • 83
  • 9
0

According to the pointer conversion rules, e.g. as described in this online c++ standard draft, any integer may be converted to a pointer value:

6.3.2.3 Pointers

(5) An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

Allowing the conversion does, however, not mean that you are allowed to dereference the pointer then.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

You can but there's a lot of considerations.

1) What does that mean?

The only really useful abstraction when this actually gets used is that you need to access a specific memory location because something is mapped to a specific point, generally hardware control registers (less often: a specific area in flash or from the linker table). The fact that you are assigning 60000 (a decimal number rather than a hexadecimal address or a symbolic mnemonic) makes me quite worried.

2) Do you have "odd" pointers?

Some microcontrollers have pointers with strange semantics (near vs far, tied to a specific memory page, etc.) You may have to do odd things to make the pointer make sense. In addition, some pointers can do strange things depending upon where they point. For example, the PIC32 series can point to the exact same data but with different upper bits that will retrieve a cached copy or an uncached copy.

3) Is that value the correct size for the pointer?

Different architectures need different sizes. The newer data types like intptr_t are meant to paper over this.

  • Point no. 3 is rather moot as `intptr_t`, `int *`, `void *` all have the same size anyway. – syockit Jul 06 '21 at 00:54
  • Most of the time. However, near vs far pointers still exist on things like the PIC18, so I'm not convinced that's a universally true statement. For example, void * could be guaranteed to be a near pointer while int * might be a far pointer. – Andrew Lentvorski Jul 07 '21 at 02:07