473,225 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

using new operator to create an array of pointers ?!

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
8 17698
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
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

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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: moller | last post by:
I'm new to perl but not to programming in general. I hav been trying to put some data into an excel sheet. And it works fine.... if I hardcode the data. And I of cource I dont want to do that....
0
by: Raju | last post by:
Hi all i am Rajendran I am working as a asp.net programmer. I am frish to this field so please any of u garify my doubt I Create array of Textbox Dynamically and not passible to retrieve data...
1
by: dragannis | last post by:
Hello, I realy need help about this, I have database that I want to use in my Vb code, and I need to create array from elements of one field from that mdb. Why : I need to do some calculations...
14
by: Rg | last post by:
Hello, everyone. I'm curious. Why can't I overload an operator only with pointers to class objects? For instance: class SomeClass { friend SomeClass *operator<<(SomeClass *, int); };
12
by: =?Utf-8?B?RGFuaWVs?= | last post by:
Hi, I have create a class name employee. Next, i would like to create array for the employee class. The code as below: dim eply(5) as employee when i call eply(0).name="Khrish", an error...
2
by: Bharath | last post by:
Hello All, Can you please let me know if we can do pointer arthrmetic using operator overloading? If not, can you please explain why it's not supported by compiler? I tried below e.g. which was...
16
by: Nkhosinathie | last post by:
hi, i'm writing a program that is using 4 arrays of pointers to char,called article,noun,verb and preposition.the program should create a sentence by selecting a word at random from each array in the...
5
by: jewel87 | last post by:
Hello, I am working on a program which uses a dynamic array of Employee class objects, the array is defined in EmployeeList class. When i am calling a function from my form to enter or display...
0
by: davaahuu | last post by:
how to create array with audio files?, Is it possible?
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.