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

Discussion in 'C++' started by wwj, Nov 3, 2003.

  1. wwj

    wwj Guest

    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.
    wwj, Nov 3, 2003
    #1
    1. Advertising

  2. wwj wrote:
    >
    > 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.


    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
    Karl Heinz Buchegger, Nov 3, 2003
    #2
    1. Advertising

  3. wwj

    JesseChen Guest

    Wonderful explaination!

    Your explaination is the most wonderful I have even encounted. Thank you!

    > int main()
    > {
    > char a[6];
    > }
    >
    > a
    JesseChen, Nov 3, 2003
    #3
  4. wwj

    Acid_X Guest

    On Mon, 03 Nov 2003 13:46:37 +0100, Karl Heinz Buchegger wrote:

    >
    >
    > wwj wrote:
    >>
    >> 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.

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


    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?
    Acid_X, Nov 3, 2003
    #4
  5. Acid_X wrote:
    >

    [snip]
    >
    > So how does one maintain access to the dynamically allocated memory?


    Easy: But not loosing the pointer which points to that memory :)

    >
    > My guess is to either have the function return a pointer to the memory,


    which function?
    There is no function in the example.

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


    If done correctly: yes. The most important thing: Don't loose the pointer.

    > Are there other methods?


    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
    Karl Heinz Buchegger, Nov 3, 2003
    #5
  6. wwj

    wwj Guest

    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 <> wrote in message news:<>...
    > wwj wrote:
    > >
    > > 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.

    >
    > 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?
    wwj, Nov 4, 2003
    #6
  7. wwj wrote:
    >
    > Thank you very much,again I want to know where the terms heap/stack
    > usually be used,


    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'


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


    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
    Karl Heinz Buchegger, Nov 4, 2003
    #7
  8. wwj

    wwj Guest

    [ Don't top-post, please. Corrected. ]
    Thank you very much.:)
    Karl Heinz Buchegger <> wrote in message news:<>...
    > wwj wrote:
    > >
    > > Thank you very much,again I want to know where the terms heap/stack
    > > usually be used,

    >
    > 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'
    >
    >
    > > and which circumstance to use char a[] or char *p=new
    > > char[].

    >
    > 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.
    wwj, Nov 5, 2003
    #8
    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. jakk
    Replies:
    4
    Views:
    11,984
  2. Santa
    Replies:
    1
    Views:
    1,051
    Mark A. Odell
    Jul 17, 2003
  3. David
    Replies:
    10
    Views:
    5,905
    Richard Heathfield
    Sep 15, 2003
  4. Replies:
    4
    Views:
    1,326
  5. wwj
    Replies:
    24
    Views:
    2,462
    Mike Wahler
    Nov 7, 2003
Loading...

Share This Page