using new operator to create an array of pointers ?!

P

ptek

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
 
R

Rolf Magnus

ptek said:
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);
 
J

Jerry Coffin

[ ... ]
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 char> than of
pointers to char.
 
P

ptek

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 said:
uchar** pointer = new unsigned char*[100];
 
P

ptek

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 said:
[ ... ]
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 char> than of
pointers to char.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
J

Jerry Coffin

[ ... top posting fixed ]
Jerry Coffin wrote:

[ ... ]
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 said:
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 char> ustring;

std::vector<ustring> p(var1*var2);

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

typedef std::basic_string<char> string;
typedef std::basic_string<wchar_t> wstring;
 
H

Howard

Jerry Coffin said:
[ ... ]
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top