473,903 Members | 4,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17722
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<uch ar*vec(100);

Sep 19 '06 #2
In article <11************ **********@k70g 2000cwa.googleg roups.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_stri ng<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<uch ar*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_stri ng<unsigned char>, i must say that my C++
needs some more studing :) Is this a template, right ?
Jerry Coffin wrote:
In article <11************ **********@k70g 2000cwa.googleg roups.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_stri ng<unsigned charthan of
pointers to char.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #5
In article <11************ **********@i42g 2000cwa.googleg roups.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_stri ng<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<wha teverp(var1*var 2);
As for the std::basic_stri ng<unsigned char>, i must say that my C++
needs some more studing :) Is this a template, right ?
std::basic_stri ng 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_stri ng<unsigned charustring;

std::vector<ust ringp(var1*var2 );

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

typedef std::basic_stri ng<charstring;
typedef std::basic_stri ng<wchar_twstri ng;

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #6
ptek <pe************ **@gmail.comwro te:
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************ **********@k70g 2000cwa.googleg roups.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%************ *********@bgtns c04-news.ops.worldn et.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
4165
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. This code works:
0
411
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 from that textbox to next window in asp .net
1
3969
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 (that will go online, while database is growing), and that´s why i need an array (off some nuber of elements). Does anybody know how can I do this ? If you can help me with this or you have some other solutions I would be greatefull.
14
1835
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
1624
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 message prompt out as saying object reference is null...
2
3204
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 giving me error. typedef class x { }X;
16
5845
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 following order:article,noun,verb preposition,,article and noun.as each word is picked,it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. the words should be separated by spaces.when the...
5
9000
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 Employee object data i'm getting errors: E2459 VCL style classes must be constructed using operator new. (I'm using Borland Builder 6) This is the code where it points to void __fastcall TForm1::CreateEmployeeButtonClick(TObject *Sender) {
0
1185
by: davaahuu | last post by:
how to create array with audio files?, Is it possible?
0
9999
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
11282
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
10873
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
10983
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
10500
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9684
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5894
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...
0
6088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4726
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

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.