Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > switch from raw pointer to vector container

Reply
Thread Tools

switch from raw pointer to vector container

 
 
markww
Guest
Posts: n/a
 
      08-30-2006
Hi,

I want to use the vector container class to store pixel data. Currently
I have some memory allocated using c++'s new operator. I allocate the
memory differently based on if the pixel type is unsigned char or
unsigned short like this:

int nPixelType = ?; // unsigned short, or unsigned char?
BYTE *pByte = NULL;
switch (nPixelType) {
case UNSIGNED_SHORT:
pBtye = (BYTE *)new unsigned char[256 * 256];
break;

case UNSIGNED_CHAR:
pByte = (BYTE *)new unsigned char[256 * 256];
break;
}

Then I whenever I was doing image processing tasks, I would create a
temporary pointer to the data and cast it to the correct type:

unsigned short *p = (unsigned short *)pByte;
or
unsigned char *p = (unsigned char *)pByte;

Now it was suggested to me that I stop using raw pointers like this in
favor of vector containers. Can I translate the above into a vector of
BYTE and accomplish the same thing? Would it be possible to get around
having to cast the pointer before accessing it everytime?

vector<BYTE> vNew;
vNew.resize(256 * 256);

Thanks

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      08-30-2006
markww wrote:
> Hi,
>
> I want to use the vector container class to store pixel data. Currently
> I have some memory allocated using c++'s new operator. I allocate the
> memory differently based on if the pixel type is unsigned char or
> unsigned short like this:
>
> int nPixelType = ?; // unsigned short, or unsigned char?
> BYTE *pByte = NULL;
> switch (nPixelType) {
> case UNSIGNED_SHORT:
> pBtye = (BYTE *)new unsigned char[256 * 256];
> break;
>
> case UNSIGNED_CHAR:
> pByte = (BYTE *)new unsigned char[256 * 256];
> break;
> }
>

This is C++ after all. What's with all the casting and switch
statements. Either make an abstract base:

class PixelData {
public:
virtual void* GetRawData() = 0;
virtual void Resize(int width, int height);
};

class UnsignedCharPixelData : public PixelData {
vector<unsigned char> uc_data;
public:
virtual void Resize(int width, int height) {
uc_data.resize(width*height);
}
virtual void* GetRawData() { return &uc_data[0]; }
};

or use a templated class:

template <typename PixelType > class PixelData {
vector<PixelType> pdata;
public:
void* GetRawData() { return &pdata[0]; }
void Resize(int w, int h) {
pdata.resize(w*h);
}
};
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      08-30-2006
"markww" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi,
>
> I want to use the vector container class to store pixel data. Currently
> I have some memory allocated using c++'s new operator. I allocate the
> memory differently based on if the pixel type is unsigned char or
> unsigned short like this:
>
> int nPixelType = ?; // unsigned short, or unsigned char?
> BYTE *pByte = NULL;
> switch (nPixelType) {
> case UNSIGNED_SHORT:
> pBtye = (BYTE *)new unsigned char[256 * 256];
> break;
>
> case UNSIGNED_CHAR:
> pByte = (BYTE *)new unsigned char[256 * 256];
> break;
> }
>
> Then I whenever I was doing image processing tasks, I would create a
> temporary pointer to the data and cast it to the correct type:
>
> unsigned short *p = (unsigned short *)pByte;
> or
> unsigned char *p = (unsigned char *)pByte;
>
> Now it was suggested to me that I stop using raw pointers like this in
> favor of vector containers. Can I translate the above into a vector of
> BYTE and accomplish the same thing? Would it be possible to get around
> having to cast the pointer before accessing it everytime?
>
> vector<BYTE> vNew;
> vNew.resize(256 * 256);
>
> Thanks


There are a few ways to accomplish this, including a class that keeps track
of the type of data it is and allocates appropriately, or templates, or
polymorphism (Pixel as base class, PixelShort or PixelChar as derived
classes), etc...

A vector container, itself, isn't a solution other than storing the data.
You still have to decide what type of data to store.


 
Reply With Quote
 
markww
Guest
Posts: n/a
 
      08-30-2006

Jim Langston wrote:
> "markww" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > Hi,
> >
> > I want to use the vector container class to store pixel data. Currently
> > I have some memory allocated using c++'s new operator. I allocate the
> > memory differently based on if the pixel type is unsigned char or
> > unsigned short like this:
> >
> > int nPixelType = ?; // unsigned short, or unsigned char?
> > BYTE *pByte = NULL;
> > switch (nPixelType) {
> > case UNSIGNED_SHORT:
> > pBtye = (BYTE *)new unsigned char[256 * 256];
> > break;
> >
> > case UNSIGNED_CHAR:
> > pByte = (BYTE *)new unsigned char[256 * 256];
> > break;
> > }
> >
> > Then I whenever I was doing image processing tasks, I would create a
> > temporary pointer to the data and cast it to the correct type:
> >
> > unsigned short *p = (unsigned short *)pByte;
> > or
> > unsigned char *p = (unsigned char *)pByte;
> >
> > Now it was suggested to me that I stop using raw pointers like this in
> > favor of vector containers. Can I translate the above into a vector of
> > BYTE and accomplish the same thing? Would it be possible to get around
> > having to cast the pointer before accessing it everytime?
> >
> > vector<BYTE> vNew;
> > vNew.resize(256 * 256);
> >
> > Thanks

>
> There are a few ways to accomplish this, including a class that keeps track
> of the type of data it is and allocates appropriately, or templates, or
> polymorphism (Pixel as base class, PixelShort or PixelChar as derived
> classes), etc...
>
> A vector container, itself, isn't a solution other than storing the data.
> You still have to decide what type of data to store.


Hi guys,

If I write a class to do it like posted, how could I keep a list of
them - eventually I want to keep the following:

vector<CPixelContainerClass> vImageStack;

the first image in the stack might have to be allocated as unsigned
char, the second might have to be allocated as unsigned short.

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      08-31-2006
On 30 Aug 2006 16:44:23 -0700 in comp.lang.c++, "markww"
<(E-Mail Removed)> wrote,
>If I write a class to do it like posted, how could I keep a list of
>them - eventually I want to keep the following:
>
> vector<CPixelContainerClass> vImageStack;


First let me say that I agree with several other posters -- if you
have those casts scattered all over your code, and deal with both
shorts and chars all over your code, your life will be miserable.
You really need to encapsulate all that into a family of classes.

Your base class would probably have something like virtual
get_pixel() and put_pixel() functions, and derived classes would
convert a common pixel representation to and from whatever more
compact form they were using internally in the implementation of
those functions.

So your vector will probably know only about an abstract base class.
And that means it has to store some kind of pointer, so that it can
point to instances of your derived classes.

The vector can store raw pointers, but that means _all_ the memory
management is up to you. Or the vector can store some kind of smart
pointer class such as boost::scoped_ptr (http://boost.org) so that
it can clean up for itself automatically.


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      08-31-2006
David Harmon wrote:
>
> The vector can store raw pointers, but that means _all_ the memory
> management is up to you. Or the vector can store some kind of smart
> pointer class such as boost::scoped_ptr (http://boost.org) so that
> it can clean up for itself automatically.
>


Or std::tr1::shared_ptr.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      08-31-2006
On Wed, 30 Aug 2006 21:09:08 -0400 in comp.lang.c++, Pete Becker
<(E-Mail Removed)> wrote,
>David Harmon wrote:
>>
>> The vector can store raw pointers, but that means _all_ the memory
>> management is up to you. Or the vector can store some kind of smart
>> pointer class such as boost::scoped_ptr (http://boost.org) so that
>> it can clean up for itself automatically.
>>

>
>Or std::tr1::shared_ptr.


But in this case it does not appear that sharing is needed.
Is there a tr1 scoped_ptr? Is tr1 a sports car from Triumph?
I know I can get a boost for free, but probably not a sports car.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      08-31-2006
"markww" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> Jim Langston wrote:
>> "markww" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ups.com...
>> > Hi,
>> >
>> > I want to use the vector container class to store pixel data. Currently
>> > I have some memory allocated using c++'s new operator. I allocate the
>> > memory differently based on if the pixel type is unsigned char or
>> > unsigned short like this:
>> >
>> > int nPixelType = ?; // unsigned short, or unsigned char?
>> > BYTE *pByte = NULL;
>> > switch (nPixelType) {
>> > case UNSIGNED_SHORT:
>> > pBtye = (BYTE *)new unsigned char[256 * 256];
>> > break;
>> >
>> > case UNSIGNED_CHAR:
>> > pByte = (BYTE *)new unsigned char[256 * 256];
>> > break;
>> > }
>> >
>> > Then I whenever I was doing image processing tasks, I would create a
>> > temporary pointer to the data and cast it to the correct type:
>> >
>> > unsigned short *p = (unsigned short *)pByte;
>> > or
>> > unsigned char *p = (unsigned char *)pByte;
>> >
>> > Now it was suggested to me that I stop using raw pointers like this in
>> > favor of vector containers. Can I translate the above into a vector of
>> > BYTE and accomplish the same thing? Would it be possible to get around
>> > having to cast the pointer before accessing it everytime?
>> >
>> > vector<BYTE> vNew;
>> > vNew.resize(256 * 256);
>> >
>> > Thanks

>>
>> There are a few ways to accomplish this, including a class that keeps
>> track
>> of the type of data it is and allocates appropriately, or templates, or
>> polymorphism (Pixel as base class, PixelShort or PixelChar as derived
>> classes), etc...
>>
>> A vector container, itself, isn't a solution other than storing the data.
>> You still have to decide what type of data to store.

>
> Hi guys,
>
> If I write a class to do it like posted, how could I keep a list of
> them - eventually I want to keep the following:
>
> vector<CPixelContainerClass> vImageStack;
>
> the first image in the stack might have to be allocated as unsigned
> char, the second might have to be allocated as unsigned short.


Well, if you want to store them that way, instead of pointers, then you
don't want to use polymorphism. So basically, just have your
CPixelContainerClass keep track of how it's stored.

enum PixelTypes
{
ShortPixels,
CharPixels
};

class CPixelContainerClass
{
public:
CPixelContainerClass( const PixelTypes pt ): PixelType_( pt ),
ShortPixel_( NULL ), CharPixel_( NULL ) {}
~CPixelContainerClass() { delete[] ShortPixel_; delete[] CharPixel_; }
PixelTypes Type() const { return PixelType_; }
void LoadData( char* Data, const int Size )
{
if ( PixelType_ == ShortPixels )
// Allocate and load data into ShortPixel_
else
// Allocate and load data into CharPixel_
}
private:
PixelTypes PixelType_;
unsigned short* ShortPixel_;
unsigned char* CharPixel_;
}

There are other ways to design this class, and maybe better ways, but you
should get the idea. You are encapsulating the data, so any class that uses
CPixelContainerClass shouldn't have to care if the data is stored in
unsigned short or unsigned char, let the class handle that trivia. If, at
some point, you do need to know outside the class, use .Type() to see how
it's stored.

This is not polymorphism, and I really don't think polymorphism is the way
to go. Personally, I might just use one pointer (unsigned char* as it's
only 1 byte) for all the data and just treat it different if PixelType_ ==
ShortPixels.

The advantage of this is you just have your vector of CPixelContainerClass
and it doesn't matter what type the data is inside of it, they are just
isntances of the same class.

I can think of off the top of my head 3 or 4 other ways to do the same
thing, such as having the pixel data itself stored in another class of
different types, etc...

It's hard to say without knowing your design constraints the best way to do
it.


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      08-31-2006
David Harmon wrote:

> Is tr1 a sports car from Triumph?


TR1 is the first Technical Report on C++ Library Extensions.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      08-31-2006
On Thu, 31 Aug 2006 06:51:43 -0400 in comp.lang.c++, Pete Becker
<(E-Mail Removed)> wrote,
>
>TR1 is the first Technical Report on C++ Library Extensions.


So I have to buy _The C++ Standard Library Extensions: a Tutorial
and Reference_ in order to find out if it has scoped_ptr?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL: valid pointer to element of a list or vector container? Rui Maciel C++ 6 05-11-2010 01:11 AM
Problem storing tvmet vector objects in an stl vector container alexjcollins@gmail.com C++ 2 09-08-2008 09:18 PM
Can list container contain an vector object, such as list< vector<string> >?? ehui928 C++ 2 05-29-2006 01:09 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments