Thread: 24 bit pointer from a 4-byte field

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339

    24 bit pointer from a 4-byte field

    On the processor I am programming on, there can be 64-bit, 31-bit and 24-bit pointers.

    I'm trying to extract a 24-bit pointer out of a 4-byte word. The first 8 bits in the 4-byte word can be flags that are set. The last 3 bytes will be a pointer, addressable for up to 16mb.

    For example, the 4-byte value might be 0x7F000010 which would point to location 0x00000010 in storage.

    I've accomplished my goal of extracting it via this code sequence:

    Code:
    #include <stdio.h>                                
    int main(void) {                                  
       void * my_32_ptr = (void *) 0x7F000010 ;
       void * my_24_ptr  ;                     
       struct {                                       
          int       : 8 ;                             
          int int24 : 24 ;                            
       } addr24 ;                                     
                                                      
       #define int24 addr24.int24                     
       int24 = (int) my_32_ptr ;                      <--- line 11 
       my_24_ptr = (void *) int24 ;            
       printf("my_24_ptr = %08X\n", my_24_ptr) ;      
       return 0 ;                                      
    }
    It works, however, I get an informational message for line 11 as follows:

    INFORMATIONAL CCN3451 MISC.C(PTR24):11 The target integral type cannot hold all possible values of the source integral type.

    At runtime, the first byte gets lopped off as desired.

    What's a better method to extract the last 3 bytes of my_32_ptr to avoid the compiler's informational message?

    My processor is little endian, so no concerns about the ordering of values in the 4-byte word.

    Thanks.
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    I see that this works.

    Code:
       my_24_ptr = (void *) ((int) my_32_ptr & (int) 0x00FFFFFF) ;
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,652
    stdint.h has types of specific sizes. In particular, it has uintptr_t which is an unsigned integer sized to hold a pointer (presumably the largest pointer type on your system). This compiles with no warnings on gcc with warnings maximized:
    Code:
    #include <stdio.h>
    #include <stdint.h>
     
    int main() {
        void *p32 = (void*)0x12345678;
     
        void *p24 = (void*)((uintptr_t)p32 & (uintptr_t)0xffffff);
        unsigned flags = (unsigned)((uintptr_t)p32 >> 24);
     
        printf("ptr  : %p\n", p24);
        printf("flags: 0x%x (", flags);
        for (unsigned bit = 0x80; bit; bit >>= 1)
            putchar(flags & bit ? '1' : '0');
        printf(")\n");
     
        return 0;
    }
    And that's the world in a nutshell, an appropriate receptacle.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    Yep, thanks. That's what I came up with.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-06-2018, 11:22 AM
  2. Read 2 Byte of a Pointer
    By ghostcoder in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2010, 02:41 AM
  3. Replies: 2
    Last Post: 06-07-2009, 08:49 AM
  4. Struct (with pointer) to byte array
    By rabencor in forum C Programming
    Replies: 1
    Last Post: 02-08-2009, 05:27 AM
  5. Pointer to BYTE
    By kshah82 in forum C Programming
    Replies: 2
    Last Post: 05-06-2003, 07:08 AM

Tags for this Thread