Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a pointer (address) ever be negative?

I have a function that I would like to be able to return special values for failure and uninitialized (it returns a pointer on success).

Currently it returns NULL for failure, and -1 for uninitialized, and this seems to work... but I could be cheating the system. IIRC, addresses are always positive, are they not? (although since the compiler is allowing me to set an address to -1, this seems strange).

[update]

Another idea I had (in the event that -1 was risky) is to malloc a char @ the global scope, and use that address as a sentinel.

like image 547
Jared Forsyth Avatar asked Jul 21 '10 23:07

Jared Forsyth


People also ask

Can a pointer address be negative?

A pointer value cannot be either positive or negative, because pointers are not numbers.

Are pointers always positive?

No, pointers are not integers. A pointer is an address.It is merely a positive number and not an integer.

Can void * be negative?

A negative void coefficient means that the reactivity decreases as the void content inside the reactor increases - but it also means that the reactivity increases if the void content inside the reactor is reduced.

Can address of pointer be null?

A null pointer is a pointer which points nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet. b) To pass a null pointer to a function argument when we don't want to pass any valid memory address.


2 Answers

No, addresses aren't always positive - on x86_64, pointers are sign-extended and the address space is clustered symmetrically around 0 (though it is usual for the "negative" addresses to be kernel addresses).

However the point is mostly moot, since C only defines the meaning of < and > pointer comparisons between pointers that are to part of the same object, or one past the end of an array. Pointers to completely different objects cannot be meaningfully compared other than for exact equality, at least in standard C - if (p < NULL) has no well defined semantics.

You should create a dummy object with static storage duration and use its address as your unintialised value:

extern char uninit_sentinel; #define UNINITIALISED ((void *)&uninit_sentinel) 

It's guaranteed to have a single, unique address across your program.

like image 138
caf Avatar answered Oct 22 '22 07:10

caf


The valid values for a pointer are entirely implementation-dependent, so, yes, a pointer address could be negative.

More importantly, however, consider (as an example of a possible implementation choice) the case where you are on a 32-bit platform with a 32-bit pointer size. Any value that can be represented by that 32-bit value might be a valid pointer. Other than the null pointer, any pointer value might be a valid pointer to an object.

For your specific use case, you should consider returning a status code and perhaps taking the pointer as a parameter to the function.

like image 33
James McNellis Avatar answered Oct 22 '22 09:10

James McNellis