Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   the difference between char a[6] and char *p=new char[6] . (http://www.velocityreviews.com/forums/t278908-the-difference-between-char-a-6-and-char-p-new-char-6-a.html)

wwj 11-03-2003 12:34 PM

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 11-03-2003 12:46 PM

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


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
kbuchegg@gascad.at

JesseChen 11-03-2003 12:57 PM

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!

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


Acid_X 11-03-2003 03:14 PM

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:

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



Karl Heinz Buchegger 11-03-2003 03:31 PM

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


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
kbuchegg@gascad.at

wwj 11-04-2003 04:04 AM

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@gascad.at> wrote in message news:<3FA64E2D.B9E029B0@gascad.at>...
> 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 11-04-2003 05:45 PM

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


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
kbuchegg@gascad.at

wwj 11-05-2003 12:59 AM

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@gascad.at> wrote in message news:<3FA7E5A8.CB6E9E71@gascad.at>...
> 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.



All times are GMT. The time now is 01:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.