By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,604 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,604 IT Pros & Developers. It's quick & easy.

pointers and references

P: n/a
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

Nov 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

zo****@gmail.com 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 [];
arrays must have a constant size.
And you don't need new.

int n;
int* p = &n;
>
and this:
int num = 123;
int arr [];
the former is just a local variable
the latter decays to a pointer if it had a constant size.
No references here.
>
Well I know that the first are pointers and the second are references.

Szymon
Not quite:

int n(123); // is a local variable
int* p = &n; // is a mutable pointer to a mutable integer
const int* p_ = &n; // is a mutable pointer to a constant integer
const int* const pp = &n; // is a constant pointer to a constant
integer
int const * const ppp = &n; // same

int& r_n = n; // is a reference to n
const int& r_n_ = n; // is a constant reference to n

Which is faster? none is faster.
Which is safer: depends on the context.

Nov 15 '06 #2

P: n/a
zo****@gmail.com 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 [];

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

Well I know that the first are pointers and the second are references.
...
There are several terminological mistakes in your question, which makes it hard
to understand, but it appears that you want to know when to use dynamic memory
as opposed to automatic (or static) memory (i.e. when to create objects with
'new' as opposed to defining them directly in local (or namespace) scope).

The rule of the thumb is: use dynamic memory when you really have to; use
automatic (or, much less often, static) memory in all other cases. The reasons
for using dynamic memory usually include situation when

1. it is necessary to have an object who's lifetime is different from
the one imposed by the rules of static or automatic storage duration (by scope,
etc.)

2. the type of the object referenced by a pointer (or a reference) is
not known at compile time (such as concrete size of an array or dynamic
type of a polymorphic object)

3. the total number of concrete objects of certain type is not known at
compile time (such as linked list nodes or tree nodes)

4. the object is to large to be placed in automatic or static memory

Dynamically allocated objects are theoretically slower to access, but in
practice the difference in access speed is more than negligible on modern
hardware platforms. Although it is worth noting that thoughtless use of dynamic
memory might lead to heap fragmentation and cache-unfriendly data layout, which
might have noticeable negative impact on program's performance.

--
Best regards,
Andrey Tarasevich


Nov 15 '06 #3

P: n/a
int* ptr = new int (123);

Asking the system to dynamically allocate memory for a single int; then
initiate that piece of memory with value 123; then point the pointer ptr
to that memory.

Dynamic memory allocation is usually considered more expensive. It
typically involves a system call to the underlying memory management
unit and going through some tricky algorithm to properly allocate that
memory, followed by updating a pointer.
int* arr = new int [];
Nothing but a syntax error.
int num = 123;
Initialize a particular memory (size of an int) in the current stack
frame with value 123. The name "num" is bind to this memory.

Allocating stack memory is usually considered a fast operation. Usually
it involves an increment to a register that identifies the usage of the
stack.

Once num is out of scope, the stack memory is reclaimed.
int arr [];
Declares a variable of type int[]. It may later be bind to an actual
storage (such as int[10].) Any use of arr before it is assigned to some
storage is harmful to your health.
Well I know that the first are pointers and the second are references.
int* ptr = new int (123); // Pointer to int
int* arr = new int []; // Pointer to int (syntax error)
int num = 123; // variable of type int
int arr []; // array of int's

None of them is a reference.
>
Szymon
Ben
Nov 16 '06 #4

P: n/a
Thanks. I think you answered my questions.

Sorry, I forgot to put the size in the arrays:
int* arr = new int [SIZE];
int arr [SIZE];

Szymon

Nov 16 '06 #5

P: n/a

benben wrote:
int* arr = new int [];

Nothing but a syntax error.
Might be confused if using VC which allows such
expression through extension.

Nov 16 '06 #6

P: n/a

dasjotre wrote:
benben wrote:
int* arr = new int [];
Nothing but a syntax error.

Might be confused if using VC which allows such
expression through extension.
What a particular version of a compiler provides as extensions are
irrelevant.
Its a syntax error.

Nov 16 '06 #7

P: n/a

Salt_Peter wrote:
dasjotre wrote:
benben wrote:
int* arr = new int [];
>
Nothing but a syntax error.
Might be confused if using VC which allows such
expression through extension.

What a particular version of a compiler provides as extensions are
irrelevant.
Its a syntax error.
doesn't change the point that he might get
confused by the answer if using VC

Nov 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.