the difference between char a[6] and char *p=new char[6] .

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

    the difference between char a[6] and char *p=new char[6] .

    Hi ,all

    I want to know the difference between char a[6] and char *p=new
    char[6] and the difference between the heap and the stack ,and if the
    char a[6] is corresponding to the stack in MEMORY,and char *p=new
    char[6] is corresponding to the heap of MEMORY.

    Give me some hint.
    THANK YOU.
  • Karl Heinz Buchegger

    #2
    Re: the difference between char a[6] and char *p=new char[6] .



    wwj wrote:[color=blue]
    >
    > Hi ,all
    >
    > I want to know the difference between char a[6] and char *p=new
    > char[6] and the difference between the heap and the stack ,and if the
    > char a[6] is corresponding to the stack in MEMORY,and char *p=new
    > char[6] is corresponding to the heap of MEMORY.
    >
    > Give me some hint.
    > THANK YOU.[/color]

    int main()
    {
    char a[6];
    }

    a
    +---+---+---+---+---+---+
    | | | | | | |
    +---+---+---+---+---+---+

    //////////////////////////////////////////////////

    int main()
    {
    char *p = new char[6];
    }

    p
    +------+ +---+---+---+---+---+---+
    | o----------------->| | | | | | |
    +------+ +---+---+---+---+---+---+


    In the first example, a is an array which is large enough to store
    6 characters.
    In the second example, p is a pointer. This pointer points to dynamically
    allocated memory, which is large enough to store 6 characters.

    As for heap vs. stack. C++ doesn't use the terms heap/stack. C++ uses the
    terms automatic storage and dynamic storage (among others). a is allocated
    on the automatic storage, meaning: whenever the variable goes out of scope,
    the memory for that variable is destroyed.
    p is also allocated on the automatic storage, but the memory where p points
    to is allocated dynamically. Such memory is released only when a correspondig
    delete [] (or delete) is done on the pointer value.

    int main()
    {
    { // start a new scope
    char a[6];

    // a
    // +---+---+---+---+---+---+
    // | | | | | | |
    // +---+---+---+---+---+---+

    } // end the scope, all variables introduced in that scope
    // are destroyed. In this case this means: no variables left
    }

    /////////////////////////////////////////////////

    int main()
    {
    { // start a new scope
    char *p = new char[6];

    // p
    // +------+ +---+---+---+---+---+---+
    // | o----------------->| | | | | | |
    // +------+ +---+---+---+---+---+---+

    } // end the scope, all variables introduced in that scope
    // are destroyed. In this case this leads to

    //
    // +---+---+---+---+---+---+
    // | | | | | | |
    // +---+---+---+---+---+---+

    // Note: The pointer p has been destroyed, since it is in automatic
    // storage. But not so the memory it has been pointing to. There is no
    // longer any pointer pointing to it: Congratulations , you have created
    // a memory leak, that memory, although still allocated to your program,
    // is inaccessible for your program.
    }


    Does that answer your questions?

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • JesseChen

      #3
      Re: the difference between char a[6] and char *p=new char[6] .

      Wonderful explaination!

      Your explaination is the most wonderful I have even encounted. Thank you!
      [color=blue]
      > int main()
      > {
      > char a[6];
      > }
      >
      > a[/color]

      Comment

      • Acid_X

        #4
        Re: the difference between char a[6] and char *p=new char[6] .

        On Mon, 03 Nov 2003 13:46:37 +0100, Karl Heinz Buchegger wrote:
        [color=blue]
        >
        >
        > wwj wrote:[color=green]
        >>
        >> Hi ,all
        >>
        >> I want to know the difference between char a[6] and char *p=new
        >> char[6] and the difference between the heap and the stack ,and if the
        >> char a[6] is corresponding to the stack in MEMORY,and char *p=new
        >> char[6] is corresponding to the heap of MEMORY.
        >>
        >> Give me some hint.
        >> THANK YOU.[/color]
        >
        > int main()
        > {
        > char a[6];
        > }
        >
        > a
        > +---+---+---+---+---+---+
        > | | | | | | |
        > +---+---+---+---+---+---+
        >
        > //////////////////////////////////////////////////
        >
        > int main()
        > {
        > char *p = new char[6];
        > }
        >
        > p
        > +------+ +---+---+---+---+---+---+
        > | o----------------->| | | | | | |
        > +------+ +---+---+---+---+---+---+
        >
        >
        > In the first example, a is an array which is large enough to store
        > 6 characters.
        > In the second example, p is a pointer. This pointer points to dynamically
        > allocated memory, which is large enough to store 6 characters.
        >
        > As for heap vs. stack. C++ doesn't use the terms heap/stack. C++ uses the
        > terms automatic storage and dynamic storage (among others). a is allocated
        > on the automatic storage, meaning: whenever the variable goes out of scope,
        > the memory for that variable is destroyed.
        > p is also allocated on the automatic storage, but the memory where p points
        > to is allocated dynamically. Such memory is released only when a correspondig
        > delete [] (or delete) is done on the pointer value.
        >
        > int main()
        > {
        > { // start a new scope
        > char a[6];
        >
        > // a
        > // +---+---+---+---+---+---+
        > // | | | | | | |
        > // +---+---+---+---+---+---+
        >
        > } // end the scope, all variables introduced in that scope
        > // are destroyed. In this case this means: no variables left
        > }
        >
        > /////////////////////////////////////////////////
        >
        > int main()
        > {
        > { // start a new scope
        > char *p = new char[6];
        >
        > // p
        > // +------+ +---+---+---+---+---+---+
        > // | o----------------->| | | | | | |
        > // +------+ +---+---+---+---+---+---+
        >
        > } // end the scope, all variables introduced in that scope
        > // are destroyed. In this case this leads to
        >
        > //
        > // +---+---+---+---+---+---+
        > // | | | | | | |
        > // +---+---+---+---+---+---+
        >
        > // Note: The pointer p has been destroyed, since it is in automatic
        > // storage. But not so the memory it has been pointing to. There is no
        > // longer any pointer pointing to it: Congratulations , you have created
        > // a memory leak, that memory, although still allocated to your program,
        > // is inaccessible for your program.
        > }
        >
        >
        > Does that answer your questions?[/color]

        So how does one maintain access to the dynamically allocated memory?

        My guess is to either have the function return a pointer to the memory, or
        pass a pointer as an argument, and inside the body of the function have
        the argument pointer point to the memory.

        Would either of those work?
        Are there other methods?


        Comment

        • Karl Heinz Buchegger

          #5
          Re: the difference between char a[6] and char *p=new char[6] .



          Acid_X wrote:[color=blue]
          >[/color]
          [snip][color=blue]
          >
          > So how does one maintain access to the dynamically allocated memory?[/color]

          Easy: But not loosing the pointer which points to that memory :-)
          [color=blue]
          >
          > My guess is to either have the function return a pointer to the memory,[/color]

          which function?
          There is no function in the example.
          [color=blue]
          > or
          > pass a pointer as an argument, and inside the body of the function have
          > the argument pointer point to the memory.
          >
          > Would either of those work?[/color]

          If done correctly: yes. The most important thing: Don't loose the pointer.
          [color=blue]
          > Are there other methods?[/color]

          Yep. Don't do dynamic memory allocation (on your own). In the posters
          case: Use a std::string instead of those character allocations.

          Look Ma! Now without me doing the memory management:

          #include <string>

          int main()
          {
          std::string MyString;

          MyString = "Hallo"; // MyString will resize itself
          MyString += " world"; // This will even work, if the string
          // gets larger this way

          std::string Copy = MyString; // The string class will also
          // do the duplication if necc.

          return 0;
          } // And the string object will delete the dynamically allcoated memory for me.
          // No action required on my side.

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • wwj

            #6
            Re: the difference between char a[6] and char *p=new char[6] .

            Thank you very much,again I want to know where the terms heap/stack
            usually be used,and which circumstance to use char a[] or char *p=new
            char[].
            Thank you again.~0~
            Karl Heinz Buchegger <kbuchegg@gasca d.at> wrote in message news:<3FA64E2D. B9E029B0@gascad .at>...[color=blue]
            > wwj wrote:[color=green]
            > >
            > > Hi ,all
            > >
            > > I want to know the difference between char a[6] and char *p=new
            > > char[6] and the difference between the heap and the stack ,and if the
            > > char a[6] is corresponding to the stack in MEMORY,and char *p=new
            > > char[6] is corresponding to the heap of MEMORY.
            > >
            > > Give me some hint.
            > > THANK YOU.[/color]
            >
            > int main()
            > {
            > char a[6];
            > }
            >
            > a
            > +---+---+---+---+---+---+
            > | | | | | | |
            > +---+---+---+---+---+---+
            >
            > //////////////////////////////////////////////////
            >
            > int main()
            > {
            > char *p = new char[6];
            > }
            >
            > p
            > +------+ +---+---+---+---+---+---+
            > | o----------------->| | | | | | |
            > +------+ +---+---+---+---+---+---+
            >
            >
            > In the first example, a is an array which is large enough to store
            > 6 characters.
            > In the second example, p is a pointer. This pointer points to dynamically
            > allocated memory, which is large enough to store 6 characters.
            >
            > As for heap vs. stack. C++ doesn't use the terms heap/stack. C++ uses the
            > terms automatic storage and dynamic storage (among others). a is allocated
            > on the automatic storage, meaning: whenever the variable goes out of scope,
            > the memory for that variable is destroyed.
            > p is also allocated on the automatic storage, but the memory where p points
            > to is allocated dynamically. Such memory is released only when a correspondig
            > delete [] (or delete) is done on the pointer value.
            >
            > int main()
            > {
            > { // start a new scope
            > char a[6];
            >
            > // a
            > // +---+---+---+---+---+---+
            > // | | | | | | |
            > // +---+---+---+---+---+---+
            >
            > } // end the scope, all variables introduced in that scope
            > // are destroyed. In this case this means: no variables left
            > }
            >
            > /////////////////////////////////////////////////
            >
            > int main()
            > {
            > { // start a new scope
            > char *p = new char[6];
            >
            > // p
            > // +------+ +---+---+---+---+---+---+
            > // | o----------------->| | | | | | |
            > // +------+ +---+---+---+---+---+---+
            >
            > } // end the scope, all variables introduced in that scope
            > // are destroyed. In this case this leads to
            >
            > //
            > // +---+---+---+---+---+---+
            > // | | | | | | |
            > // +---+---+---+---+---+---+
            >
            > // Note: The pointer p has been destroyed, since it is in automatic
            > // storage. But not so the memory it has been pointing to. There is no
            > // longer any pointer pointing to it: Congratulations , you have created
            > // a memory leak, that memory, although still allocated to your program,
            > // is inaccessible for your program.
            > }
            >
            >
            > Does that answer your questions?[/color]

            Comment

            • Karl Heinz Buchegger

              #7
              Re: the difference between char a[6] and char *p=new char[6] .



              wwj wrote:[color=blue]
              >
              > Thank you very much,again I want to know where the terms heap/stack
              > usually be used,[/color]

              Eg. in 'sloppy C++ speak' or in assembler or stack based languages (eg. Forth) or ...

              But in this newsgroup just use the term 'automatic' or 'dynamic' and you are fine.
              Sidenote: Did you know that there is a keyword 'auto' in C++. It is used as in

              auto int c;

              and has the very same meaning as if you wrote

              int c;

              That's why it isn't used much. But it remembers that this variable is
              'automatic'

              [color=blue]
              > and which circumstance to use char a[] or char *p=new
              > char[].[/color]

              Well. If you know in advance how many elements you need for the
              array, you use:

              char a[6]; // Look Ma. 6 characters are enough for me.

              The tricky thing is, that the size of the array in the above has to
              be a compile time constant. The compiler has to know how much memory
              to set aside for your array.

              In

              char* p = new a [ x ];

              the x can be any expression, even something the user has entered to your
              program: (error checking omitted for simplicity)

              int x;
              cout << "How many elements do you want ";
              cin >> x;
              char* p = new char [ x ];

              ...

              delete [] p;


              But of course it's much simpler to just use std::vector or std::string and
              forget about memory requirements. std::vector or std::string will resize
              themselfs as needed.

              --
              Karl Heinz Buchegger
              kbuchegg@gascad .at

              Comment

              • wwj

                #8
                Re: the difference between char a[6] and char *p=new char[6] .

                [ Don't top-post, please. Corrected. ]
                Thank you very much.:)
                Karl Heinz Buchegger <kbuchegg@gasca d.at> wrote in message news:<3FA7E5A8. CB6E9E71@gascad .at>...[color=blue]
                > wwj wrote:[color=green]
                > >
                > > Thank you very much,again I want to know where the terms heap/stack
                > > usually be used,[/color]
                >
                > Eg. in 'sloppy C++ speak' or in assembler or stack based languages (eg. Forth) or ...
                >
                > But in this newsgroup just use the term 'automatic' or 'dynamic' and you are fine.
                > Sidenote: Did you know that there is a keyword 'auto' in C++. It is used as in
                >
                > auto int c;
                >
                > and has the very same meaning as if you wrote
                >
                > int c;
                >
                > That's why it isn't used much. But it remembers that this variable is
                > 'automatic'
                >
                >[color=green]
                > > and which circumstance to use char a[] or char *p=new
                > > char[].[/color]
                >
                > Well. If you know in advance how many elements you need for the
                > array, you use:
                >
                > char a[6]; // Look Ma. 6 characters are enough for me.
                >
                > The tricky thing is, that the size of the array in the above has to
                > be a compile time constant. The compiler has to know how much memory
                > to set aside for your array.
                >
                > In
                >
                > char* p = new a [ x ];
                >
                > the x can be any expression, even something the user has entered to your
                > program: (error checking omitted for simplicity)
                >
                > int x;
                > cout << "How many elements do you want ";
                > cin >> x;
                > char* p = new char [ x ];
                >
                > ...
                >
                > delete [] p;
                >
                >
                > But of course it's much simpler to just use std::vector or std::string and
                > forget about memory requirements. std::vector or std::string will resize
                > themselfs as needed.[/color]

                Comment

                Working...