pointers

Z

zombek

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
 
V

Victor Bazarov

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
 
R

Rolf Magnus

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

David Harmon

On 15 Nov 2006 14:53:57 -0800 in comp.lang.c++, (e-mail address 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.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top