access to hardware. 
Author Message
 access to hardware.

Hello,

When accessing a hardware device (residing at 0xaa55, say)- I believe I've
seen something like:

:
int *hwPointer;
hwPointer=(int *) 0xaa55;    // force pointer to point at hardware location
*hwPointer= 0xffff                 // data for hardware location
:

It all seems to make sense, apart from the cast!
I was hoping someone could explain the reason for the cast operation, or to
put it another way,

doesn't
:
hwPointer=0xaa55;
:
make the pointer point to address 0xaa55..?

Many thanks
David




Mon, 28 Oct 2002 03:00:00 GMT  
 access to hardware.
Quote:

> Hello,

> When accessing a hardware device (residing at 0xaa55, say)- I believe I've
> seen something like:

> :
> int *hwPointer;
> hwPointer=(int *) 0xaa55;    // force pointer to point at hardware location
> *hwPointer= 0xffff                 // data for hardware location
> :

> It all seems to make sense, apart from the cast!
> I was hoping someone could explain the reason for the cast operation, or to
> put it another way,

> doesn't
> :
> hwPointer=0xaa55;
> :
> make the pointer point to address 0xaa55..?

Yes it does, but you'll probably get a warning from your compiler
that there is some conversion from an integer into a pointer, therefor
using a cast will make sure you definitly want to point that pointer
to the address 0xaa55 but do not want to assign a value to the
memory location pointed to by hwPointer!

        Z



Mon, 28 Oct 2002 03:00:00 GMT  
 access to hardware.


Quote:
> Hello,

> When accessing a hardware device (residing at 0xaa55, say)- I believe
I've
> seen something like:

> :
> int *hwPointer;
> hwPointer=(int *) 0xaa55;    // force pointer to point at hardware
location
> *hwPointer= 0xffff                 // data for hardware location
> :

> It all seems to make sense, apart from the cast!
> I was hoping someone could explain the reason for the cast operation,
or to
> put it another way,

> doesn't
> :
> hwPointer=0xaa55;
> :
> make the pointer point to address 0xaa55..?

   No, it doesn't. Actually, it doesn't compile because of type
mismatch - you are assigning integer value to variable which has
the type "pointer to int".

--
        Regards,
                Alex Krol
Disclaimer: I'm not speaking for CreoScitex, Creo and/or Scitex Corp.
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 28 Oct 2002 03:00:00 GMT  
 access to hardware.


Quote:

> When accessing a hardware device (residing at 0xaa55, say)- I believe
I've
> seen something like:

> :
> int *hwPointer;
> hwPointer=(int *) 0xaa55;    // force pointer to point at hardware
location
> *hwPointer= 0xffff                 // data for hardware location

Hi David Fussell,

The problem with the above assignment is, that the result is highly
platform specific. ANSI-C says that assigning an integral value to a
pointer has implementation defined result. In other words each compiler
will interpret the assignment in it's own specific way. Only detailed
knowledge about the target hardware and the compiler will let you know
what actually results from the assignment.

Consider that on a processor with strict memory protection an invalid
pointer value might cause a processor exception right at the assignment.

Quote:
> It all seems to make sense, apart from the cast!

Say what ? The cast explicitely states that you boldly order the
compiler to use a specific integer value as a memory address. The
resulting pointer might not make much sense, but the assignment and the
cast sure do.

Quote:
> hwPointer=0xaa55;
> :
> make the pointer point to address 0xaa55..?

Nope, because in C you must follow some basic type compatibility rules.
You can not assign incompatible types. Basic numeric data types and
pointers are inherently incompatible. If you want to make the assignment
anyway you must force the conversion by supplying a cast.

--
Stephan
bringing the c.l.c campaign against grumpiness into the next millennium
fight the bug, read the FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 28 Oct 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Accessing the hardware

2. Access Audio Hardware

3. Hardware accessing

4. Accessing Hardware Port

5. Accessing Hardware!!!

6. Intercept hardware access

7. Fast access to memory mapped hardware

8. access violations and Direct3D with hardware graphics

9. Hardware-Access + Interrupt-Handling in Win32

10. port hardware access

11. Accessing Hardware

12. Accessing Hardware Resources

 

 
Powered by phpBB® Forum Software