2

How can I access an interrupt vector located at the machine's location 0? If I set a pointer to 0, the compiler might translate it to some nonzero internal null pointer value.

0

3 Answers 3

2

If you're working in a domain where you need to read/write vectors at address zero (I guess you're on some kind of embedded system?), then you will find lots of what you're doing will be outside a general straight-down-the-middle interpretation of the 'C' standard.

You can pretty safely assume that embedded compilers will generate accesses to the addresses you put into pointers, and not magically change their contents.

However, you can't always assume that a physical address zero in a datasheet is what the processor accesses when you read/write address zero. If there's an MMU in the processor, then you might need to go through a logical -> physical mapping process of some kind, and even if there isn't a full MMU, many modern small embedded processor play games with the address space around interrupt vectors (booting out of flash and then optionally remapping that part of the address space to RAM, for example.)

1

You are correct that depending on your C implementation, the internal null pointer might not be 0. But is usually is. If you want to be very safe, you can typecast the value, or use memset().

uintptr_t zerobits = 0;
void *pointer1 = (void *)zerobits;

or:

void *pointer2;
memset(&pointer2, 0, sizeof pointer2);
3
  • In most (embedded) machine types I've worked with, memory address 0 was perfectly valid and not the same as the null pointer address. The C standard requires that the number 0 be converted to the internal null pointer address though, which keeps most code portable.
    – nmichaels
    Jul 27, 2010 at 22:23
  • @Nathon, I've had the opposite experience - 0 is almost always both a valid pointer and the null pointer address.
    – Carl Norum
    Jul 27, 2010 at 23:28
  • how is that possible? Either the machine fires an interrupt when a memory address is accessed or it doesn't. C guarantees that it will convert the address 0 (at compile time) into the machine representation of the null pointer address.
    – nmichaels
    Jul 28, 2010 at 11:46
1

Since whatever is at location 0 is obviously machine dependent, you're free to use whatever machine-dependent trick will work to get there. Read your vendor's documentation. It's likely that if it's at all meaningful for you to be accessing location 0, the system will be set up to make it reasonably easy to do so. Some possibilities are:

Simply set a pointer to 0. (This is the way that doesn't have to work, but if it's meaningful, it probably will.) Assign the integer 0 to an int variable, and convert that int to a pointer. (This is also not guaranteed to work, but it probably will.) Use a union to set the bits of a pointer variable to 0:

union {
    int u_p;
    int u_i;    / assumes sizeof(int) >= sizeof(int *) */
} p;

p.u_i = 0;

Use memset to set the bits of a pointer variable to 0:

memset((void *)&p, 0, sizeof(p));

Declare an external variable or array

extern int location0;

and use an assembly language file, or some special linker invocation, to arrange that this symbol refers to (i.e. the variable is placed at) address 0.

2
  • You should use unsigned instead of int. Also, you're assuming that the user program won't produce a memory fault if address zero is read. Jul 27, 2010 at 21:26
  • 1
    That's the question's assumption.
    – aib
    Jul 27, 2010 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.