pointers and references

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
 
S

Salt_Peter

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

Andrey Tarasevich

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

benben

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.

Ben
 
Z

zombek

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
 
D

dasjotre

Salt_Peter said:
dasjotre said:
benben said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top