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

W

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.
 
K

Karl Heinz Buchegger

wwj said:
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?
 
J

JesseChen

Wonderful explaination!

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

Acid_X

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

Karl Heinz Buchegger

Acid_X said:
[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.
 
W

wwj

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 said:
wwj said:
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?
 
K

Karl Heinz Buchegger

wwj said:
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.
 
W

wwj

[ Don't top-post, please. Corrected. ]
Thank you very much.:)
Karl Heinz Buchegger said:
wwj said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top