cast a pointer

M

markww

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 (nSamplesPerPixel == 1) {
p = (unsigned short *)pData;
}
else if (nSamplesPerPixel == 3) {
p = (unsigned char *)pData;
}

How can I do that?

Thanks
 
F

Frederick Gotham

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;
};
};
 
H

Howard

markww said:
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 (nSamplesPerPixel == 1) {
p = (unsigned short *)pData;
}
else if (nSamplesPerPixel == 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
 
R

Ron Natalie

Frederick said:
markww posted:



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_cast
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.
 
R

Ron Natalie

Frederick said:
markww posted:



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.
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top