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 !
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 !