memory pointer tricks

L

loudking

data is a pointer with data-type char pointing to an address starting
with 0x81F4 blabla

what I what is to get 0x1F4 out of data.

if data is plused by one, I will get 0xF4 blabla

if data is casted to short, I will get 0xF481

if data is "AND" with 0xfff, I will get a segmentation vialation.

Could anybody tell me what to do please?

Thanks in advance.
 
E

Eric Sosman

loudking said:
data is a pointer with data-type char pointing to an address starting
with 0x81F4 blabla

what I what is to get 0x1F4 out of data.

Subtract 0x8000blabla.
 
J

James Kuyper

loudking said:
data is a pointer with data-type char pointing to an address starting
with 0x81F4 blabla

What precisely do you mean by that? How did you determine what the
address is? In particular, what does the code look like that you used to
print out that string of hexedecimal digits, and what does the "blabla"
actually represent? Several things you say down below suggest that what
you're looking at is not what I'd expect, but I haven't been able to
figure out what your are looking at.
what I what is to get 0x1F4 out of data.

Why? The reason that I ask is that most of the likely reasons for
wanting to look at the actual representation of a pointer are bad ones.
In particular, why are you interested in extracting only 12 bits of the
pointer?

I'm assuming that you want to extract 12 bits. Your example is also
consistent with wanting to extract as few as 7 consecutive bits, or 6
non-consecutive bits, or a number of other even less likely operations.
That's one of the problems with describing what you want to do by giving
an example, rather than by actually explaining it.
if data is plused by one, I will get 0xF4 blabla

That is rather odd, depending upon what you mean by "0x81F4 blabla".
if data is casted to short, I will get 0xF481

Converting a pointer to a integer type other than intptr_t or uintptr_t
is seldom meaningful. Even conversion to one of those two types is not
portable, though it often has a non-portable meaning.
if data is "AND" with 0xfff, I will get a segmentation vialation.

Attempting to apply binary & on a pointer is a constraint violation, and
most implementations won't even compiler such code. If an implementation
does choose to compile it, the resulting behavior is undefined, by
reason of the absence of a specific definition for the behavior. So a
segmentation violation is entirely possible, though it seems very unlikely.

I suspect that you're not correctly describing what your code actually
does; a segmentation violation suggests that you tried to store the
result of your AND operation in a pointer, and then tried to use that
pointer. No matter what you did to create that pointer value, if it even
vaguely resembles what you've been telling us, it's not guaranteed to be
a valid pointer value, and is pretty likely to be invalid.

It would be a lot clearer if you give us the actual code that produced
the segmentation violation.
Could anybody tell me what to do please?

In the unlikely circumstance that you actually have a good reason for
doing this, you're probably using an implementation where conversion of
a pointer to uintptr_t will give you a number that, in hexadecimal,
looks like "0x81F4 blabla", as you put it. In that case, you can just
perform bitwise manipulations on the uintptr_t variable to extract 0x1F4.
 
G

Guest

data is a pointer with data-type char pointing to an address starting
with 0x81F4 blabla

I think you mean "... with data-type char* ...". Or just
"... with type char* ...". And it *contains* the address 0x81FAxxxx
or has the value of, rather than points to.

So you have something like this:-
char *data = (char*)0x81F40000;
what I what is to get 0x1F4 out of data.

why? You are trying to do something highly non-portable.
Does this do what you want?

long ll = (long)data; /* assume 32-bit addresses */
long extract = (ll & 0x0FFF0000) >> 0x10000;
if data is plused by one, I will get 0xF4 blabla

no you won't you'll get 0x81F40001. Hmm. Maybe I've
completely misunderstood you. Do you mean data points to an address
containing the value 0x81F4...? Could you post some code?
if data is casted to short, I will get 0xF481

on some implementations
if data is "AND" with 0xfff, I will get a segmentation vialation.

I don't see why
Could anybody tell me what to do please?

tell us what your real problem is. Why do you want to
extract 3 nibbles from the middle of an address?
 
G

Guest

[...]
Does this do what you want?
   long ll = (long)data;  /* assume 32-bit addresses */
   long extract = (ll & 0x0FFF0000) >> 0x10000;

     Probably not, unless he's running on a system where `long'
is at least 65537 bits wide.  Even then, he'll just get zero.

     (We'll take your "Oops!" as read ...)

yes... Quite why I wanted to shift by a number so large
I've no idea.
 
R

Richard Bos

loudking said:
Could anybody tell me what to do please?

Yes. Show code. You're not being clear in describing what you _have_
tried, so show, don't tell.
And probably, stop confusing the pointer with what it points _at_.

Richard
 
L

lawrence.jones

loudking said:
data is a pointer with data-type char pointing to an address starting
with 0x81F4 blabla

what I what is to get 0x1F4 out of data.

int i = ((((unsigned char *)data)[0] << 8) |
((unsigned char *)data)[1]) & 0x1ff;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top