By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

using new operator to create an array of pointers ?!

P: n/a
Hi all,

I'm quite new to pointers, so this might be a silly question :S
I need to allocate an array of pointers to unsigned char type...

So, if I needed instead to allocate an array of unsigned chars, i'll do
this :

uchar *pointer = new unsigned [100];
Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];
What is wrong with this code ?
Does the new operator will do for this ?
Or should I used malloc ?

Thanks

Sep 19 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ptek wrote:
Hi all,

I'm quite new to pointers, so this might be a silly question :S
I need to allocate an array of pointers to unsigned char type...
So, if I needed instead to allocate an array of unsigned chars, i'll do
this :

uchar *pointer = new unsigned [100];
This shouldn't work due to a type mismatch.
>

Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];
What is wrong with this code ?
If you allocate an array of uchar, new will give you back a pointer to
uchar. Now guess what it will give you if you allocate an array of pointers
to uchar. Also, the syntax for a pointer to unsigned char is not
*unsigned char. Try:

uchar** pointer = new unsigned char*[100];
Does the new operator will do for this ?
Yes.
Or should I used malloc ?
You should use neither. Instead try a vector.

std::vector<uchar*vec(100);

Sep 19 '06 #2

P: n/a
In article <11**********************@k70g2000cwa.googlegroups .com>,
pe**************@gmail.com says...

[ ... ]
Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];
That needs to be:

unsigned char *pointer = new unsigned char *[100];

Though, of course we're left wondering why you're using dynamic
allocation at all. Chances are pretty good you'd be better off with a
vector -- and quite possibly of std::basic_string<unsigned charthan of
pointers to char.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #3

P: n/a

Hi Rolf
Thanks for your answer.

Man, I was really dumb to swap unsigned char* for *unsigned char
....
And believe it or not, i've tried the unsigned char** pointer
previously, but with the other error, of course ...

As for the std::vector, that's pretty unknown to me ... I think i'll
stick to the new operator for now. Is there advantages to use the
std::vector thing instead of the new ?

tnx
Rolf Magnus wrote:
>
uchar** pointer = new unsigned char*[100];
>
You should use neither. Instead try a vector.

std::vector<uchar*vec(100);
Sep 19 '06 #4

P: n/a
Well, the 100 was just an example : in fact, i need to allocate
quantity that is a product of two variables...

As for the std::basic_string<unsigned char>, i must say that my C++
needs some more studing :) Is this a template, right ?
Jerry Coffin wrote:
In article <11**********************@k70g2000cwa.googlegroups .com>,
pe**************@gmail.com says...

[ ... ]
Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];

That needs to be:

unsigned char *pointer = new unsigned char *[100];

Though, of course we're left wondering why you're using dynamic
allocation at all. Chances are pretty good you'd be better off with a
vector -- and quite possibly of std::basic_string<unsigned charthan of
pointers to char.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #5

P: n/a
In article <11**********************@i42g2000cwa.googlegroups .com>,
pe**************@gmail.com says...

[ ... top posting fixed ]
Jerry Coffin wrote:
[ ... ]
Though, of course we're left wondering why you're using dynamic
allocation at all. Chances are pretty good you'd be better off with a
vector -- and quite possibly of std::basic_string<unsigned charthan of
pointers to char.

Well, the 100 was just an example : in fact, i need to allocate
quantity that is a product of two variables...
Okay -- that would justify some sort of dynamic allocation, but it still
sounds like you could use an std::vector to do the job:

std::vector<whateverp(var1*var2);
As for the std::basic_string<unsigned char>, i must say that my C++
needs some more studing :) Is this a template, right ?
std::basic_string is a template. This is an instantiation of the
template to produce a class. In a typical case, you'd probably want to
do something like:

typedef std::basic_string<unsigned charustring;

std::vector<ustringp(var1*var2);

In fact, std::string and std::wstring are just like this:

typedef std::basic_string<charstring;
typedef std::basic_string<wchar_twstring;

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #6

P: n/a
ptek <pe**************@gmail.comwrote:
As for the std::vector, that's pretty unknown to me ... I think i'll
stick to the new operator for now. Is there advantages to use the
std::vector thing instead of the new ?
See the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-34.1

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 19 '06 #7

P: n/a

"Jerry Coffin" <jc*****@taeus.comwrote in message
news:MP************************@news.sunsite.dk...
In article <11**********************@k70g2000cwa.googlegroups .com>,
pe**************@gmail.com says...

[ ... ]
>Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];

That needs to be:

unsigned char *pointer = new unsigned char *[100];
Actually, that should be:

unsigned char ** pointer = new unsigned char *[100];

-Howard
Sep 19 '06 #8

P: n/a
In article <T%*********************@bgtnsc04-news.ops.worldnet.att.net>,
al*****@hotmail.com says...

[ ... ]
Actually, that should be:

unsigned char ** pointer = new unsigned char *[100];
Oops -- thanks for the correction.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.