3

Possible Duplicate:
Can a pointer (address) ever be negative?

I'm considering initialising a structure to all -1s with memset(since it uses no signed numbers and zero is a valid value).

Is -1 a valid pointer adress? and are there any other problems with my idea? note: platform is linux/gcc/x86

P.S. I'm trying to initialize a struct that is not all pointers and where zero is valid to all invalid like values so I can optionally do partial initialization in one function and initialise the non initialised fields to default values later on. If there is a pattern/strategy to do this in c?

Community
  • 1
  • 1
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141

4 Answers4

4

The interpretation of -1 as a pointer is architecture-dependent and therefore unreliable.

In general, memset is intended to set bytes, not pointers. C does not make any guarantees as to how individual bytes combine to make a pointer. Even if your solution works, you'll have to document how and why it works.

A better idea, when NULL is a valid value, is to set all pointers to a sentinel of an appropriate type. So, if your structure has a field int *ip:

static const int sentineli;

// in the initialization:
foo->ip = (int *)&sentineli;

then compare with that value. This is self-documenting.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • They fields are not all pointers – Roman A. Taycher Mar 21 '11 at 11:50
  • 2
    @Roman: initializing a pointer with `memset` is still a very bad idea. – Fred Foo Mar 21 '11 at 11:53
  • This is the times when architecture definitions come in handy. You can just use #ifdef DESKTOP_ARCHITECTURE INVALID_POINTER (void*)1 #elifdef MICRO_SYSTEM_ARCHITECTURE INVALID_POINTER – user13947194 Mar 03 '23 at 19:20
  • On Windows, I know not Linux, applications valid pointers are actually ranged. Something like 0xffff0000 is valid pointers. 0xffff is reserved for integers like string resources. check out MAKE INT RESOURCE for win32 for more info. Pretty you could store pointers in a 16 bit integer and flags in another 16 bit integer. .... OOps I forgot that 0xffff0001 is still valid pointer. – user13947194 Mar 03 '23 at 21:25
3

In general, the only valid pointer values are NULL, and pointers to the beginning, inside, and right after, an existing object. Also note that NULL does not necessarily have to be represented as a bit-pattern with zero:s.

On some architectures, -1 is a legal pointer value. Some microcontrollers have RAM in the low part of memory, and read-only memory (like flash) in the upper parts.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
1

Negative numbers are usually stored in implementation defined manner. Some implementations use 1's complement to store negative numbers, others use 2's complement.

and are there any other problems with my idea?

The code might not be portable across implementations.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • -1 used with `memset` will always be the all-one-bits pattern, because the -1 is converted to `unsigned char`. Of course that does not necessarily match the integer value -1, and there is no such thing as a pointer value of -1 in C. – R.. GitHub STOP HELPING ICE Mar 21 '11 at 13:28
0

On any real world desktop or server system, -1 cast to a pointer is equivalent to the all-bits-one pointer representation, and it is not a valid pointer. Assuming pointer addition takes place as integer addition, if a pointer p has the all-bits-one representation, p+1 is going to be the all-bits-zero pointer, which in the real world is the null pointer.

Nonetheless, none of this is guaranteed by the standard.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711