473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

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.
Jul 19 '05 #1
7 16977


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
kb******@gascad.at
Jul 19 '05 #2
Wonderful explaination!

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

a

Jul 19 '05 #3
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?
Jul 19 '05 #4


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
kb******@gascad.at
Jul 19 '05 #5
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 <kb******@gascad.at> wrote in message news:<3F***************@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?

Jul 19 '05 #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
kb******@gascad.at
Jul 19 '05 #7
wwj
[ Don't top-post, please. Corrected. ]
Thank you very much.:)
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@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.

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul | last post by:
Say I have: char foo; char** bar; In my understanding, these two have the same meaning in memory. Could someone elucidate upon these two types and their meanings? Thanks, Paul
24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
7
by: iamchiaweilin | last post by:
Hello all: What's the difference between p and q in the following statements? char p = "Hello"; char *q = "Hello"; I know q stores the address of 'H'. Question is: does p store the...
13
by: In a little while | last post by:
thanks
3
by: artifact.one | last post by:
Hi. What's the practical difference between: #include <header.h> and: #include "header.h"
13
by: manish sahu | last post by:
difference between calloc() and malloc()
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.