Why using the new keyword at all for memory allocation?

P

PencoOdStip

Why using the new keyword at all for memory allocation?

I mean,why not let c++ find memory for you variable automatically?

so insted of :

int proba;
proba = new int[5];

why not just :

int proba[5];

????????????
 
H

Howard

Why using the new keyword at all for memory allocation?

I mean,why not let c++ find memory for you variable automatically?

so insted of :

int proba;
proba = new int[5];

why not just :

int proba[5];

Well, that's the preferred way to do it, assuming you know at compile time
how much memory you'll need to allocate. But what if you don't? How do you
allocate a memory chunk if you don't know until run-time how much memory you
need?

There are also times when you allocate one new object at a time, in response
to some sort of event, and add the new'ed pointer to a container which
handles its objects over their lifetime.

There are ways around using new even in these cases (such as a "pool"), but
those are generally used to solve observed problems with dynamic allocation
in very specific cases.

You might also look into "smart pointers", which can make life much easier
on you when it comes to maintaining dynamically-allocated objects.

-Howard
 
V

Victor Bazarov

Why using the new keyword at all for memory allocation?

Several reasons. Lifetime control, for one. Dynamic sizing.
Free store usually has more room than the place where automatic
variables live.
I mean,why not let c++ find memory for you variable automatically?

so insted of :

int proba;
proba = new int[5];

Actually it's more like

int *proba = new int[5];
why not just :

int proba[5];

????????????

In many cases, if all you need is an array of _exactly_ 5 integers,
and it is only needed in this particular scope, that's how you'd do
it. However, if the size is only known at the run-time, you are
either stuck with an array that is large enough to accommodate any
needed size (which means wasting memory in many cases) or got to do
dynamic allocation.

V
 
J

James Kanze

Why using the new keyword at all for memory allocation?
I mean,why not let c++ find memory for you variable automatically?
so insted of :
int proba;
proba = new int[5];
why not just :
int proba[5];
????????????

Lifetime.

First, of course, you'd never write the former; it won't even
compile. There are cases where the second might be used, when
you know the exact size of the array, and it is a compile time
constant, but something like:
std::vector< int > proba( 5 ) ;
is much more frequent and useful. (And I can't think of a case
where you'd ever use "new int[5]".)

But both the first and the second have automatic storage
duration if they are declared as local variables, and static
storage duration if they are declared at namespace scope (or if
you add the specifier "static" before the declaration of a local
variable). Automatic storage duration follows scope; the
variable ceases to exist when you leave the scope in which it
was defined. And static storage duration is the lifetime of the
program. If this doesn't match the requirements, you have to
manage lifetime yourself, manually, which means new and delete,
e.g.:

std::vector< int >* proba = NULL ;
// ...
proba = new std::vector< int >( 5 ) ;
// ...
delete proba ;

While this occurs very, very rarely for more or less basic
types, like int or vector, it is the usual case for entity
objects.
 
K

Kar

Actually C++ find memory at three different places.

1.stack
2.heap
3.free-store

In stack :any thing without new key word here { int proba[5]; }
In free-store : use as you mentioned
In heap : use malloc funtion

Wishes
Kar
 
G

Gavin Deane

Please don't top-post. Thank you. Rearranged.
Why using the new keyword at all for memory allocation?
I mean,why not let c++ find memory for you variable automatically?
so insted of :
int proba;
proba = new int[5];
why not just :
int proba[5];

Actually C++ find memory at three different places.

1.stack
2.heap
3.free-store

Actually, C++ finds memory in one place - the computer on which the
program is running. There are however three different types of
*storage duration* that C++ defines.

1. Automatic storage duration (which is almost certainly what you are
referring to as "stack").
2. Dynamic storage duration (which is almost certainly what you are
referring to as "heap" and "free-store").
3. Static storage duration (which you missed out).
In stack :any thing without new key word here { int proba[5]; }
In free-store : use as you mentioned
In heap : use malloc funtion

That's not correct.

In terms of storage duration there is no distinction between new and
malloc. Objects that live in memory allocated via new or via malloc
will have dynamic storage duration.

Objects defined at namespace scope or with the static storage modifier
will have static storage duration.

Anything else will have automatic storage duration.

Gavin Deane
 
S

Stuart Redmann

Why using the new keyword at all for memory allocation?

I mean,why not let c++ find memory for you variable automatically?

so insted of :

int proba;
proba = new int[5];

why not just :

int proba[5];

????????????

If think what the poster is trying to ask is why we should need to care about
memory allocations at all. Consider MatLab for example. There you can use any
identifier like an matrix without having to specify the dimensions beforehand,
and the interpreter will automatically allocate space if you assign an element
that lies outside the currently allocated space.

Similiar behaviour can be found in std::vector, for instance (the vector can
grow dynamically, so you don't have to know its size at compile time). You
should keep in mind, that such data structures like std::vector can cause severe
performance penalties if used careless (the standard example of careless use of
dynamically growing data structures is when you add one element at a time to a
CString from MFC 6.0. Each time you do this the underlying buffer must be
re-allocated and copied!)

Regards,
Stuart
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top