Connecting Tech Pros Worldwide Forums | Help | Site Map

question about operator new

carpenti
Guest
 
Posts: n/a
#1: Jan 22 '06
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]=i;

delete [] a;
return 0;
}





Your
advice would be greatly appreciated !



Mateusz Łoskot
Guest
 
Posts: n/a
#2: Jan 22 '06

re: question about operator new


carpenti wrote:[color=blue]
> In the following piece of code, is the use of
> the operator new strictly necessary?[/color]

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.
[color=blue]
> can I safely replace the line "a=new double[n];"
> with "double a[n]", and keep "delete [] a;" to
> free the reserved memory ?[/color]

No.

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Victor Bazarov
Guest
 
Posts: n/a
#3: Jan 22 '06

re: question about operator new


carpenti wrote:[color=blue]
> 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 ?[/color]

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[]'.
[color=blue]
> 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.[/color]

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[]".

[color=blue]
>
> 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]=i;
>
> delete [] a;
> return 0;
> }[/color]

V


Karthi
Guest
 
Posts: n/a
#4: Feb 19 '06

re: question about operator new


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










"carpenti" <bruno.carpentieri@uni-graz.at> wrote in message
news:dr0ccf$7d3$1@skirner.kfunigraz.ac.at...[color=blue]
> 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]=i;
>
> delete [] a;
> return 0;
> }
>
>
>
>
>
> Your
> advice would be greatly appreciated !
>
>[/color]


peter koch
Guest
 
Posts: n/a
#5: Feb 19 '06

re: question about operator new



carpenti skrev:
[color=blue]
> 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]=i;
>
> delete [] a;
> return 0;
> }
>
>
>
>
>
> Your
> advice would be greatly appreciated ![/color]

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

Closed Thread


Similar C / C++ bytes