Discussion:
the difference between char a[6] and char *p=new char[6] .
(too old to reply)
wwj
2003-11-03 12:34:01 UTC
Permalink
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
2003-11-03 12:46:37 UTC
Permalink
Post by wwj
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
***@gascad.at
JesseChen
2003-11-03 12:57:26 UTC
Permalink
Wonderful explaination!

Your explaination is the most wonderful I have even encounted. Thank you!
Post by Karl Heinz Buchegger
int main()
{
char a[6];
}
a
Acid_X
2003-11-03 15:14:31 UTC
Permalink
Post by Karl Heinz Buchegger
Post by wwj
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
2003-11-03 15:31:11 UTC
Permalink
Acid_X wrote:
[snip]
Post by Acid_X
So how does one maintain access to the dynamically allocated memory?
Easy: But not loosing the pointer which points to that memory :-)
Post by Acid_X
My guess is to either have the function return a pointer to the memory,
which function?
There is no function in the example.
Post by Acid_X
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.
Post by Acid_X
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
***@gascad.at
wwj
2003-11-04 04:04:37 UTC
Permalink
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~
Post by Karl Heinz Buchegger
Post by wwj
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
2003-11-04 17:45:12 UTC
Permalink
Post by wwj
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'
Post by wwj
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
***@gascad.at
wwj
2003-11-05 00:59:00 UTC
Permalink
[ Don't top-post, please. Corrected. ]
Thank you very much.:)
Post by Karl Heinz Buchegger
Post by wwj
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;
int c;
That's why it isn't used much. But it remembers that this variable is
'automatic'
Post by wwj
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
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.
Loading...