Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointers

Reply
Thread Tools

pointers

 
 
zombek@gmail.com
Guest
Posts: n/a
 
      11-15-2006
Hi.
I'd like to know which is faster and when to use:

this (int type is just an example I mean it generally):
int* ptr = new int (123);
int* arr = new int [];

and this:
int num = 123;
int arr [];

Well I know that the first are pointers and the second are references.

Szymon

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'd like to know which is faster and when to use:
>
> this (int type is just an example I mean it generally):
> int* ptr = new int (123);
> int* arr = new int [];


The former allocates only one int. The latter is a syntax error.

>
> and this:
> int num = 123;
> int arr [];
>
> Well I know that the first are pointers and the second are references.


The former declares/defines/initialises a single int. The latter
declares an array of int of unknown size. These are semantically
different.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-15-2006
(E-Mail Removed) wrote:

> Hi.
> I'd like to know which is faster and when to use:
>
> this (int type is just an example I mean it generally):
> int* ptr = new int (123);
> int* arr = new int [];


The second line is incorrect. You have to specify a size for the array.

> and this:
> int num = 123;
> int arr [];


Again, you have to specify a size for the array.

> Well I know that the first are pointers and the second are references.


There are no references involved at all.
The first example shows dynamically allocated objects. You can use them if
you want to control the life time manually. The object will exist until you
use delete on the pointer. Use this only if you really need to.

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      11-16-2006
On 15 Nov 2006 14:53:57 -0800 in comp.lang.c++, (E-Mail Removed) wrote,
>Well I know that the first are pointers and the second are references.


The latter are not references, but instead they are the real thing,
actual variables. That's generally what you want.

Don't use pointers unless you have a real reason for them.
Don't use 'new' unless you really have a real reason and the
standard library containers like std::vector won't do the job.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Does deleting a container of pointers also delete the (contained) pointers? Xamalek C++ 7 11-04-2003 04:17 PM
c++: pointers to pointers A C++ 3 10-29-2003 01:15 PM
pointers to pointers // exception handling error muser C++ 3 09-18-2003 06:19 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments