473,396 Members | 2,098 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,396 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 17704
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.