cast a pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • markww

    #1

    cast a pointer

    Hi,

    I have a block of data that was saved as either unsigned shorts or
    unsigned chars. I want to make a single pointer to it so I don't have
    to keep casting.

    BYTE *pData; // the original data, could be stored as unsigned
    shorts or unsigned chars.

    // A generic pointer to the data, which I want to interpret
    depending on spp:
    void *p = NULL;

    // Here interpret it differently?
    if (nSamplesPerPix el == 1) {
    p = (unsigned short *)pData;
    }
    else if (nSamplesPerPix el == 3) {
    p = (unsigned char *)pData;
    }

    How can I do that?

    Thanks

  • Frederick Gotham

    #2
    Re: cast a pointer

    markww posted:
    Hi,
    >
    I have a block of data that was saved as either unsigned shorts or
    unsigned chars. I want to make a single pointer to it so I don't have
    to keep casting.

    I suppose you could start off with something like:

    struct Pointer {
    enum Type {Char, Short} type;

    union {
    char unsigned *pc;
    short unsigned *ps;
    };
    };

    --

    Frederick Gotham

    Comment

    • Howard

      #3
      Re: cast a pointer


      "markww" <markww@gmail.c omwrote in message
      news:1156533316 .222063.3930@75 g2000cwc.google groups.com...
      Hi,
      >
      I have a block of data that was saved as either unsigned shorts or
      unsigned chars. I want to make a single pointer to it so I don't have
      to keep casting.
      >
      BYTE *pData; // the original data, could be stored as unsigned
      shorts or unsigned chars.
      >
      // A generic pointer to the data, which I want to interpret
      depending on spp:
      void *p = NULL;
      >
      // Here interpret it differently?
      if (nSamplesPerPix el == 1) {
      p = (unsigned short *)pData;
      }
      else if (nSamplesPerPix el == 3) {
      p = (unsigned char *)pData;
      }
      >
      Obviously, casting pData and assigning it to a void* isn't gong to do
      anything. Drop the cast if you want to use a void*. But that's not what
      you want, right?

      Have you looked into using templates? They're great for this kind of thing.

      -Howard






      Comment

      • Ron Natalie

        #4
        Re: cast a pointer

        Frederick Gotham wrote:
        markww posted:
        >
        >Hi,
        >>
        >I have a block of data that was saved as either unsigned shorts or
        >unsigned chars. I want to make a single pointer to it so I don't have
        >to keep casting.
        >
        >
        I suppose you could start off with something like:
        >
        struct Pointer {
        enum Type {Char, Short} type;
        >
        union {
        char unsigned *pc;
        short unsigned *ps;
        };
        };
        >
        CAREFUL. I had to clean up hundreds of lines of bullshit
        code that used that construct. If you store a char* into
        one pointer and retrieved it from the unsigned, it may not
        be converted properly. At least with reinterpret_cas t
        you get a fighting chance.

        The DELELCOR HEP that we ported 4 BSD to encoded the operand
        size in the pointer. Our casting operators in the compiler
        could convert the pointers appropriately, but the kernel as
        written used the "convert-by-union" which led to bizarre
        effects.

        Comment

        • Ron Natalie

          #5
          Re: cast a pointer

          Frederick Gotham wrote:
          markww posted:
          >
          >Hi,
          >>
          >I have a block of data that was saved as either unsigned shorts or
          >unsigned chars. I want to make a single pointer to it so I don't have
          >to keep casting.
          >
          >
          I suppose you could start off with something like:
          >
          struct Pointer {
          enum Type {Char, Short} type;
          >
          union {
          char unsigned *pc;
          short unsigned *ps;
          };
          };
          >
          class Pointer {
          void* internal;
          public:
          Pointer(char unsigned*);
          Pointer(short unsinged*);
          char unsigned* get_puc();
          short unsigned* get_suc();
          };

          Implementation is left as an exercise to the student.

          Comment

          Working...