Keil Logo

C51: Pointer Storage Size


Information in this article applies to:

  • C51 version 5.50 and later

QUESTION

I have defined a pointer which points to a variable in XDATA memory. Why does the pointer takes up three bytes of memory, surely only two bytes are required?

ANSWER

You are using a generic pointer. Generic pointers may point to any memory space (IDATA, XDATA, CODE, etc.). For example:

unsigned char xdata foo;
unsigned char *foo_ptr = &foo;

The first byte of the pointer indicates the type of memory the pointer points to. For example, 01H is xdata. The remaining two bytes are of course the address.

You may declare memory-specific pointers instead of generic pointers if you want more efficient pointer operations. When declaring the pointer, include the memory area it points to. In the case of a pointer to XDATA, only two bytes are required for storage. For example:

unsigned char xdata foo;
unsigned char xdata *foo_ptr = &foo;

The code generated by using a memory-specific pointer is smaller and faster than a generic pointer, however, the generic pointer may reference any memory area.

Both memory-specific pointers and generic pointers may be stored in any memory area.

MORE INFORMATION

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.