473,837 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch from raw pointer to vector container

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<BYTEvNew ;
vNew.resize(256 * 256);

Thanks

Aug 30 '06 #1
14 3656
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 UnsignedCharPix elData : public PixelData {
vector<unsigned charuc_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<PixelTyp epdata;
public:
void* GetRawData() { return &pdata[0]; }
void Resize(int w, int h) {
pdata.resize(w* h);
}
};
Aug 30 '06 #2
"markww" <ma****@gmail.c omwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.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<BYTEvNew ;
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.
Aug 30 '06 #3

Jim Langston wrote:
"markww" <ma****@gmail.c omwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.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<BYTEvNew ;
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<CPixelCo ntainerClassvIm ageStack;

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

Aug 30 '06 #4
On 30 Aug 2006 16:44:23 -0700 in comp.lang.c++, "markww"
<ma****@gmail.c omwrote,
>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<CPixelCo ntainerClassvIm ageStack;
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_p tr (http://boost.org) so that
it can clean up for itself automatically.
Aug 31 '06 #5
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_p tr (http://boost.org) so that
it can clean up for itself automatically.
Or std::tr1::share d_ptr.
Aug 31 '06 #6
On Wed, 30 Aug 2006 21:09:08 -0400 in comp.lang.c++, Pete Becker
<pe********@acm .orgwrote,
>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_p tr (http://boost.org) so that
it can clean up for itself automatically.

Or std::tr1::share d_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.

Aug 31 '06 #7
"markww" <ma****@gmail.c omwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
>
Jim Langston wrote:
>"markww" <ma****@gmail.c omwrote in message
news:11******* *************** @e3g2000cwe.goo glegroups.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<BYTEvNew ;
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<CPixelCo ntainerClassvIm ageStack;

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
CPixelContainer Class keep track of how it's stored.

enum PixelTypes
{
ShortPixels,
CharPixels
};

class CPixelContainer Class
{
public:
CPixelContainer Class( const PixelTypes pt ): PixelType_( pt ),
ShortPixel_( NULL ), CharPixel_( NULL ) {}
~CPixelContaine rClass() { 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
CPixelContainer Class 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 CPixelContainer Class
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.
Aug 31 '06 #8
David Harmon wrote:
Is tr1 a sports car from Triumph?
TR1 is the first Technical Report on C++ Library Extensions.
Aug 31 '06 #9
On Thu, 31 Aug 2006 06:51:43 -0400 in comp.lang.c++, Pete Becker
<pe********@acm .orgwrote,
>
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?

Aug 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1642
by: Tommy Lang | last post by:
Hi! I am trying to write a function that goes through an array of objects(class Student) and checks if object name(student name)matches search string(char *). I want the function to return a pointer to the object, if a match is found. This is my code so far...(am I even close? :-)) //I want to return a ponter of type student
11
3047
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs using ::pointer? I did a quick experiment and replaced all the ::iterator in my code with ::pointer and it seems to work the same. What I'm think is on the surface they're the same but perhaps there're some subtle differences beneath the...
7
1927
by: john townsley | last post by:
when using vectors with pointers of objects. is it best to create instances then point to them or dynamic allocation upon creating a vector element. I can do this, but is creating a vector with pointer to objects best done like this...with dynamic allocation vector<emp_class*> ptvector; ptvector.push_back(new emp_class(4,"ee")); cout<<ptvector->....
34
4190
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
18
2571
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct std::vector<Entry>::iterator modelIterator; In a method, I am currently writing, I need to get a pointer to an entry in the vector.. I thought of the following:
19
1652
by: Michael | last post by:
Hi, typedef std::vector<Vehicle* VehicleList; Vehicle is a UDT, or a class. Why is Vehicle* not just Vehicle in < >. Which one is better and why? If you could explain it by laying out some codes, it would be highly appreciated! Thanks in advance, Michael
2
35651
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
18
1827
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
13
2083
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is shared. I was told the new standards are being finalized and I am hoping minor but important changes could be applied before anything else. To give a quick overview on the current status of this topic, you will find below the latest text...
0
9839
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10564
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6998
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.