question about operator new

Discussion in 'C++' started by carpenti, Jan 22, 2006.

  1. carpenti

    carpenti Guest

    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 !
    carpenti, Jan 22, 2006
    #1
    1. Advertising

  2. carpenti wrote:
    > 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
    --
    Mateusz Ã…oskot
    http://mateusz.loskot.net
    =?UTF-8?B?TWF0ZXVzeiDFgW9za290?=, Jan 22, 2006
    #2
    1. Advertising

  3. carpenti wrote:
    > 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
    Victor Bazarov, Jan 22, 2006
    #3
  4. carpenti

    Karthi Guest

    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" <> wrote in message
    news:dr0ccf$7d3$...
    > 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 !
    >
    >
    Karthi, Feb 19, 2006
    #4
  5. carpenti

    peter koch Guest

    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
    peter koch, Feb 19, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dodo
    Replies:
    1
    Views:
    356
    Ron Natalie
    Aug 26, 2003
  2. Jef Driesen
    Replies:
    1
    Views:
    492
    Gernot Frisch
    Jan 19, 2005
  3. Replies:
    8
    Views:
    322
    Neil Cerutti
    Dec 22, 2005
  4. xmllmx
    Replies:
    6
    Views:
    405
    xmllmx
    Feb 3, 2010
  5. Replies:
    2
    Views:
    442
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page