question about operator new

C

carpenti

Dear all,

I am learning C++ and I got a little confused
about dynamic allocation of arrays. Any help
is greatly appreciated.
In the following piece of code, is the use of
the operator new strictly necessary ? I mean,
can I safely replace the line "a=new double[n];"
with "double a[n]", and keep "delete [] a;" to
free the reserved memory ? Many thanks in advance.

Best,
Bruno


~~~~~~~~~~~~~~~~~~~

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

int n,i;
cin >> n;
double *a;

a=new double[n];

for (i=0; i<n ; i++)
a=i;

delete [] a;
return 0;
}





Your
advice would be greatly appreciated !
 
G

Guest

carpenti said:
In the following piece of code, is the use of
the operator new strictly necessary?

Yes, because n value is dynamic and not known at compile time.
So you need to allocate your array dynamically using new operator.

After all, you need to deallocate it (array) using delete[] operator.
can I safely replace the line "a=new double[n];"
with "double a[n]", and keep "delete [] a;" to
free the reserved memory ?

No.

Cheers
 
V

Victor Bazarov

carpenti said:
I am learning C++ and I got a little confused
about dynamic allocation of arrays. Any help
is greatly appreciated.
In the following piece of code, is the use of
the operator new strictly necessary ?

Yes. There is no other way to have an array of unknown
size. An alternative is to use 'std::vector', but under
the covers it still uses 'new[]'.
I mean,
can I safely replace the line "a=new double[n];"
with "double a[n]", and keep "delete [] a;" to
free the reserved memory ? Many thanks in advance.

No. If your compiler allows you to define an automatic
array with the size from a non-constant expression, and
some do (it's a language extension), you would not need
to use "delete[]".

Best,
Bruno


~~~~~~~~~~~~~~~~~~~

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

int n,i;
cin >> n;
double *a;

a=new double[n];

for (i=0; i<n ; i++)
a=i;

delete [] a;
return 0;
}


V
 
K

Karthi

Hi,
First of all, new and delete keywords are used for allocating memory in
heap. Means, it will be dynamically allocated. But if you use double a[n],
it will be allocated in Stack Memory. Real purpose of dynamic allocation
will be destroyed. You may use malloc instead. If you are storing in stack
you are not supposed to delete it. It ll be deleted, right off it goes out
of scope in program execution.
Karthikeyan S
 
P

peter koch

carpenti skrev:
Dear all,

I am learning C++ and I got a little confused
about dynamic allocation of arrays. Any help
is greatly appreciated.
In the following piece of code, is the use of
the operator new strictly necessary ? I mean,
can I safely replace the line "a=new double[n];"
with "double a[n]", and keep "delete [] a;" to
free the reserved memory ? Many thanks in advance.

Best,
Bruno


~~~~~~~~~~~~~~~~~~~

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

int n,i;
cin >> n;
double *a;

a=new double[n];

for (i=0; i<n ; i++)
a=i;

delete [] a;
return 0;
}





Your
advice would be greatly appreciated !


As said by others, n is not a compile-time constant, you do need to use
an array. My advice would be to use std::vector instead: a program that
calls delete - and delete [] in particular - should always raise your
suspicion: those programs are not using the advantage of C++. Thus
replace the lines
double *a;
a = new double[n]

with std::vector a(n)

and remove the delete[] statement.

/Peter
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top