Page 1 of 1
error: invalid operands to binary &
Posted: Fri Sep 21, 2007 1:53 pm
by FlashBurn
I get this error because I want to "and" a pointer to test if it is aligned on a 4kb pg.
Is there another way to do that? At the moment I do:
Which does not work
Posted: Fri Sep 21, 2007 2:17 pm
by eboyd
first of all, what language is this?
Posted: Fri Sep 21, 2007 2:23 pm
by eboyd
if it's c/c++ 2 "&"'s are used, like so:
But what you have doesn't seem valid:
Might I suggest:
Code: Select all
if((p != 0) && (0xfff != 0))
{
...
}
I omitted the "*" becasue I', assuming "p" is a pointer anyway.
Posted: Fri Sep 21, 2007 2:43 pm
by FlashBurn
I want to take the value of the pointer (the address) "and" it with 0xfff to look if the address is aligned!
There has to be a way how I can do this.
The language I use is "C"!
Re: error: invalid operands to binary &
Posted: Fri Sep 21, 2007 2:59 pm
by Alboin
Posted: Fri Sep 21, 2007 3:57 pm
by Brynet-Inc
Clearly 'eboyd' doesn't know anything about bitwise operations..
Posted: Fri Sep 21, 2007 5:40 pm
by bluecode
I think what you want is
where p is the pointer (say a 'void*'), right?
Posted: Fri Sep 21, 2007 9:21 pm
by FlashBurn
Yeah, but how do you define uintptr_t?
And my pointer is a ptr to a struct and then then pointer which is in the struct:
Code: Select all
struct test_t
{
void *base;
};
struct test_t *p;
if((p->base & 0xfff) != 0)
//code
If I make this:
Code: Select all
uint32_t temp;
temp= p->base;
if((temp &0xfff) != 0)
//code
It does compile, but is it also working?
Posted: Fri Sep 21, 2007 9:59 pm
by pcmattman
Code: Select all
struct test_t
{
void *base;
};
struct test_t *p;
if((p->base & 0xfff) != 0)
//code
Are you trying to find out if the address of 'p' is page aligned, or if the address pointed to by 'base' is page-aligned?
Posted: Sat Sep 22, 2007 2:31 am
by bluecode
FlashBurn wrote:Yeah, but how do you define uintptr_t?
Depends on your Compiler and on the architecture you are targeting. The only requirements of an uintptr_t are that
a) if you convert a pointer to a uintptr_t and back to a pointer you do not loose information
b) uintptr_t should be of integer type (so that you can do math with it)
For GCC + x86 or x86-64 'unsigned long' does the thing. On GCC + x86 'unsigned long' is 32bit and on GCC + x86-64 it is 64bit, so in every case it matches the size of a pointer.
@FlashBurn: Your last code snippet looks ok to me (But I would use uintptr_t for more portability). Just to clarify: C does not offer binary operations for pointers, so you have to cast the pointer to a type where you can do binary operations.
Posted: Sat Sep 22, 2007 3:26 am
by Solar
bluecode wrote:FlashBurn wrote:Yeah, but how do you define uintptr_t?
Depends on your Compiler and on the architecture you are targeting.
Actually, no.
You define uintptr_t like this:
Well, at least if you're working in a C99 environment.
Posted: Sat Sep 22, 2007 6:47 am
by FlashBurn
Ok, I solved it with a typcast to an uint32_t.
Posted: Sat Sep 22, 2007 6:58 am
by bluecode
Solar wrote:Actually, no.
You define uintptr_t like this:
Well, at least if you're working in a C99 environment.
Actually I was thinking he is using this in his OS and does not have a full C99 environment and has to provide his own C99 environment.
But thanks for mentioning that what I said should happen within stdint.h.
Posted: Sat Sep 22, 2007 7:26 am
by eboyd
Clearly 'eboyd' doesn't know anything about bitwise operations.. Laughing
Thanks. I love being publicly ridiculed.
But you're right. I don't use bitwise operations. I didn't realize that's what his post was all about.
Posted: Mon Sep 24, 2007 6:49 am
by Solar
bluecode wrote:Actually I was thinking he is using this in his OS and does not have a full C99 environment and has to provide his own C99 environment.
As he posted to "General Programming"... besides, it's hard to remember how much of an environment everyone's OS provides. For him, the solution was to typedef uint32_t... had I suggested that, it would have sounded a bit silly if he didn't have an uint32_t, right?