2

My question is in regards to dealing with address spaces.

I have two address spaces which are in Hex: 0x7fffff09 and 0x7fffff08.

How can I know if they are divisible by 8 or 8 byte aligned ? Like what would the check look like in C or C++ code. I know you usually you use mod for regular numbers and if there is no remainder then you know it is divisible.

Edit: Address space can be __8, __16, __32 (8 bits, 16 bits, 32 bits)

Masterminder
  • 1,127
  • 7
  • 21
  • 31
  • Are you sure those are address *spaces* and not just regular old *addresses* (aka pointers)? If so, exactly what hardware and OS are we talking about? – zwol Nov 05 '13 at 15:47
  • @Masterminder: You need to clarify your question. How is that "address" represented? By an integer? Or by a pointer? – AnT stands with Russia Nov 05 '13 at 15:51
  • possible duplicate of [Which is the best way, in C, to see if a number is divisible by another?](http://stackoverflow.com/questions/6129827/which-is-the-best-way-in-c-to-see-if-a-number-is-divisible-by-another) – Butzke Nov 05 '13 at 15:55
  • If one uses `%` as part of the solution: "... The operands of the % operator shall have integer type." C11dr 6.5.5 2. So the address must be converted to some integer type before `%` is applied. – chux - Reinstate Monica Nov 05 '13 at 16:17

4 Answers4

7

8 byte aligned depends on the architecture.

8 byte divisible is just a modulo operation. Copy the pointer to a large enough int and do % 8.

#include <stdint.h>
void *addr = ....
uintptr_t i = (uintptr_t)addr;
i = i % 8

You can, of course, put this in a function:

int isEightByteDivisible(const void *addr) {
    return ((uintptr_t)addr % 8) == 0
}

See also Determining the alignment of C/C++ structures in relation to its members for alignment issues.

Community
  • 1
  • 1
Charlie Burns
  • 6,994
  • 20
  • 29
3

The remainder operator is %. So simply test

addr % 8 == 0

If you have the address in the form of a pointer, you need to cast the pointer to an appropriate unsigned integer type.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I'm not sure if that will work since it is in hex do I need to do some sort of casting and if yeah how would that look like? – Masterminder Nov 05 '13 at 15:48
  • 2
    What do you mean "it is in hex"? Are you aware that converting a number's representation from binary to hex to decimal etc. does not change the value of the number. A number is either divisible by 8, or not. It does not matter how you write or represent the number. – David Heffernan Nov 05 '13 at 15:49
  • 2
    @Masterminder: the base in which you represent a number has no bearing on the operations you can do on it or on its properties. `0xf`==`15`==`017`, and in memory they are all just stored in binary. (to nitpickers: I am aware that hex integral literals are unsigned by default, thank you very much) – Matteo Italia Nov 05 '13 at 15:50
  • if the address space is in __8, or __16, or __32 , will the modulo operation work still or do i need to do a casting? – Masterminder Nov 05 '13 at 15:52
  • What I was asking was, will the % operation work on unsigned integer types of 8 bits, 16 bits, 32 bits? – Masterminder Nov 05 '13 at 15:54
  • 1
    Still, you *do* need a cast to some kind of integer if `addr` is actually a pointer. – Matteo Italia Nov 05 '13 at 15:57
2

You can do it quickly with the bitwise & operator. You will have to first cast to uintptr_t:

bool isAligned(const void *ptr) {
    return !(((uintptr_t)ptr) & 7);
}
Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • Although probably I too would write it with `& 7` for bonus street cred, it must be noted that even writing it with the modulo operator produces the same assembly output on any decent compiler (compilers nowadays perform sick tricks with arithmetic, this is one of the easiest). – Matteo Italia Nov 05 '13 at 16:06
1
if (addr % 8 == 0)
{
    //divisible by 8
}
else
{
    //not divisible by 8
}
Butzke
  • 561
  • 1
  • 14
  • 30
  • This answer is incomplete. If `addr` is a pointer, it should be cast to `uintptr_t` to avoid compiler warnings or even errors. – Andy J Apr 16 '21 at 04:49