Any difference in these array declarations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vasileios Zografos

    #1

    Any difference in these array declarations

    Hello,
    can anyone please tell me if there is any difference between the two:


    double Array1[10];


    and


    double* Array2 = new double[10];
    ..
    ..
    ..
    delete [] Array2;


    Thanks


  • Neelesh Bodas

    #2
    Re: Any difference in these array declarations


    Vasileios Zografos wrote:[color=blue]
    > Hello,
    > can anyone please tell me if there is any difference between the two:
    >
    > double Array1[10];
    > and
    > double* Array2 = new double[10];
    > delete [] Array2;[/color]

    Array1 is name of an array, a constant. Array2 is simply a pointer to a
    double. Array2 can be reassigned a different value, Array1 cannot be.

    double pi = 3.14;
    Array2 = & pi; // fine
    Array1 = & pi// error. Incompatible types in assignment.

    Comment

    • Victor Bazarov

      #3
      Re: Any difference in these array declarations

      Vasileios Zografos wrote:[color=blue]
      > can anyone please tell me if there is any difference between the two:
      >
      >
      > double Array1[10];
      >
      >
      > and
      >
      >
      > double* Array2 = new double[10];
      > .
      > .
      > .
      > delete [] Array2;[/color]

      Of course there is. 'Array1' has the type 'array of 10 double', whereas
      'Array2' has the type 'pointer to double'.

      V

      Comment

      • Tsubasa

        #4
        Re: Any difference in these array declarations

        different method of memory allocation,
        array1 is allocated on stack,
        array2 is allocated on heap.

        Comment

        • Vasileios Zografos

          #5
          Re: Any difference in these array declarations


          "Tsubasa" <chanmaosheng@g mail.com> wrote in message
          news:1136393754 .956822.96010@g 47g2000cwa.goog legroups.com...[color=blue]
          > different method of memory allocation,
          > array1 is allocated on stack,
          > array2 is allocated on heap.[/color]

          Thanks ;)


          Comment

          • Neelesh Bodas

            #6
            Re: Any difference in these array declarations


            Tsubasa wrote:[color=blue]
            > different method of memory allocation,
            > array1 is allocated on stack,[/color]
            not always. May be in data section, if it is declared non-locally.

            Comment

            • Victor Bazarov

              #7
              Re: Any difference in these array declarations

              Tsubasa wrote:[color=blue]
              > different method of memory allocation,
              > array1 is allocated on stack,[/color]

              I presume you meant 'Array1', not 'array1'. And where it's allocated
              depends entirely on where the declaration appears.
              [color=blue]
              > [..][/color]

              And please quote the relevant portion of the post you're replying to.

              Comment

              • red floyd

                #8
                Re: Any difference in these array declarations

                Vasileios Zografos wrote:[color=blue]
                >
                > double Array1[10];
                >
                > double* Array2 = new double[10];
                > .
                > .
                > .
                > delete [] Array2;
                >[/color]

                Array1 is exception safe. Array2 is not exception safe if an uncaught
                exception is thrown in the "..." section of code.

                Comment

                • Victor Bazarov

                  #9
                  Re: Any difference in these array declarations

                  Neelesh Bodas wrote:[color=blue]
                  > Tsubasa wrote:
                  >[color=green]
                  >>different method of memory allocation,
                  >>array1 is allocated on stack,[/color]
                  >
                  > not always. May be in data section, if it is declared non-locally.
                  >[/color]

                  Just to pick a nit, neither 'stack', nor 'data section', nor even 'heap'
                  are proper C++ terms for the area of memory where objects live. It would
                  be better to use 'automatic', 'static', or 'dynamic' to distinguish the
                  objects. The only thing known is that dynamic objects are allocated in
                  the "free store".

                  V

                  Comment

                  • Frinos

                    #10
                    Re: Any difference in these array declarations

                    OK here goes file mou... I will try and explain the difference without
                    getting too technical,

                    Declaring an array as "double Array1[10] can be thought of as declaring
                    a "hard coded" array... i.e. it's size MUST be known at the time of
                    compilation.... For example,

                    const int CONST_VAL = 5;
                    int nVal = 6;

                    double Array1[10]; // valid
                    double Array2[CONST_VAL]; // also valid
                    double Array3[nVal]; // NOT valid, nVal is not known
                    at compilation!!


                    Declaring an array as "double* pArray2 = new double[10]" has the same
                    effect with the main difference being the size of the array can be
                    stated from the value contained in a variable.... For example,

                    const int CONST_VAL = 5;
                    int nVal = 6;

                    double* pArray1 = new double[10]; // valid
                    double* pArray2 = new double[CONST_VAL]; // also valid
                    double* pArray3 = new double[nVal]; // also valid


                    Whichever way the array is created, it is still accessed in the same
                    way

                    double Array1[10]; // Array 1
                    double pArray2 = new double[10]; // Array 2

                    for( int i = 0; i < 10; i++ )
                    {
                    Array1[ i ] = i;
                    pArray2[ i ] = i;
                    }

                    Both arrays will contain exactly the same data...

                    BUT remember, any memory created using the "new" keyword MUST be
                    deleted when finished with otherwise you risk aquiring memory
                    leaks!!... And also "hard coded" arrays do not require deletion as they
                    are automatically deleted when out of scope!!...

                    So Array 2 requires to be deleted as so when not required further,

                    delete pArray2; (or delete [] pArray2;)

                    Regards

                    Frinos



                    Vasileios Zografos wrote:[color=blue]
                    > Hello,
                    > can anyone please tell me if there is any difference between the two:
                    >
                    >
                    > double Array1[10];
                    >
                    >
                    > and
                    >
                    >
                    > double* Array2 = new double[10];
                    > .
                    > .
                    > .
                    > delete [] Array2;
                    >
                    >
                    > Thanks[/color]

                    Comment

                    • Gavin Deane

                      #11
                      Re: Any difference in these array declarations

                      Please do no top-post. Thank you. Rearranged.


                      Frinos wrote:[color=blue]
                      > Vasileios Zografos wrote:[color=green]
                      > > Hello,
                      > > can anyone please tell me if there is any difference between the two:
                      > > double Array1[10];
                      > > and
                      > > double* Array2 = new double[10];
                      > > .
                      > > .
                      > > .
                      > > delete [] Array2;[/color]
                      > OK here goes file mou... I will try and explain the difference without
                      > getting too technical,
                      >
                      > Declaring an array as "double Array1[10] can be thought of as declaring
                      > a "hard coded" array... i.e. it's size MUST be known at the time of
                      > compilation.... For example,
                      >
                      > const int CONST_VAL = 5;
                      > int nVal = 6;
                      >
                      > double Array1[10]; // valid
                      > double Array2[CONST_VAL]; // also valid
                      > double Array3[nVal]; // NOT valid, nVal is not known
                      > at compilation!![/color]

                      Side issue - don't use ALL_CAPS identifiers for anything except macros.
                      That way you protect yourself from macros destroying your code before
                      the compiler sees it.
                      [color=blue]
                      > Declaring an array as "double* pArray2 = new double[10]" has the same
                      > effect with the main difference being the size of the array can be
                      > stated from the value contained in a variable.... For example,
                      >
                      > const int CONST_VAL = 5;
                      > int nVal = 6;
                      >
                      > double* pArray1 = new double[10]; // valid
                      > double* pArray2 = new double[CONST_VAL]; // also valid
                      > double* pArray3 = new double[nVal]; // also valid[/color]

                      double* pArray2 = new double[10] does *not* declare an array. It
                      declares a pointer and initialises that pointer with the address of the
                      first element of an array of 10 doubles. Maybe that's more technical
                      than you were trying to be, but it's important to note that, by your
                      declarations, Array is an array while pArray is a pointer.

                      Arrays and pointers are not the same thing.

                      <snip>
                      [color=blue]
                      > BUT remember, any memory created using the "new" keyword MUST be
                      > deleted when finished with otherwise you risk aquiring memory
                      > leaks!!... And also "hard coded" arrays do not require deletion as they
                      > are automatically deleted when out of scope!!...
                      >
                      > So Array 2 requires to be deleted as so when not required further,
                      >
                      > delete pArray2; (or delete [] pArray2;)[/color]

                      You seem to suggest that either of those options is valid.

                      delete pArray2 is incorrect. delete and delete [] are different things.
                      Mixing them up causes undefined behavior. Every new must be matched
                      with a delete. Every new [] (as you have been using) must be matched
                      with a delete [].

                      Gavin Deane

                      Comment

                      • Tsubasa

                        #12
                        Re: Any difference in these array declarations

                        Tsubasa wrote:[color=blue]
                        > different method of memory allocation,
                        > array1 is allocated on stack,[/color]
                        [color=blue]
                        >not always. May be in data section, if it is declared non-locally.[/color]

                        "in data section"? you mean public variable or static variable?

                        Comment

                        • Neelesh Bodas

                          #13
                          Re: Any difference in these array declarations


                          Tsubasa wrote:
                          [color=blue][color=green]
                          > > different method of memory allocation,
                          > > array1 is allocated on stack,[/color]
                          >[color=green]
                          > >not always. May be in data section, if it is declared non-locally.[/color]
                          >
                          > "in data section"? you mean public variable or static variable?[/color]

                          I meant that automatic storage is allocated on stack , and global or
                          static storatge is allocated in data segment. But as Victor suggested,
                          "stack, data, heap" are not terms used by std C++. So to rephrase, what
                          I meant was that the location where the storage is allocated depends on
                          the place where ithe variable's declaration appears. (automatic /static
                          storage area and free store are probably better terms)

                          Comment

                          • Rennie deGraaf

                            #14
                            Re: Any difference in these array declarations

                            Tsubasa wrote:[color=blue]
                            > Tsubasa wrote:
                            >[color=green]
                            >>different method of memory allocation,
                            >>array1 is allocated on stack,[/color]
                            >
                            >[color=green]
                            >>not always. May be in data section, if it is declared non-locally.[/color]
                            >
                            >
                            > "in data section"? you mean public variable or static variable?
                            >[/color]

                            On some systems at least, globals, statics, constants, string literals,
                            etc. will be stored in a "data" section that is separate from the stack
                            and heap. There may even be separate sections for read-only and
                            writable globals.

                            Of course, this is all system-dependent, and C++ knows nothing about any
                            of it.

                            Rennie deGraaf

                            Comment

                            • Frinos

                              #15
                              Re: Any difference in these array declarations

                              Sorry for the previous top post, (I'm sure you can excuse first time
                              posters!!.... )...

                              With regards to using CAPS, there are no do's and dont's.... it's just
                              preference and following your coding standards....

                              With regards to declaring the array I suppose its bad use of words on
                              my part, maybe I should have used the word "create" rather than
                              declare.....
                              In one case you declare the array (which also creates it... double
                              Array[10])..... In the other case, you declare a pointer to a double
                              and assign it with the address of the first element of the "newly"
                              created array... double* pArray = new double[10]).... In both cases an
                              array is created but the important thing to note is that with the first
                              array, its memory is reserved during compilation and with the other
                              array, it is created dynamically at run time and therefore the memory
                              reserved for it must be deleted...

                              With regards to array deletion your statement is not necessarily the
                              case, delete can be used just as well as delete[] when used with the
                              new[] operator...... It all depends on your compiler and if it allows
                              it. I think the C++ standards have changed in this area..... I use
                              MSVC++6.0 and does not give me errors or undefined behaviour, but I do
                              guess its better to get into the habit of using delete[] with new[]...

                              Regards

                              Frinos

                              Comment

                              Working...