473,796 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17000


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******@gasca d.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******@gasca d.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
1606
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
3637
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 *p=new char is corresponding to the heap of MEMORY. Give me some hint. THANK YOU.
9
13118
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 am curious how compiler find it.
10
15665
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
61
3162
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 with "" it is - correct? The system seems to handle a null the same as "".
7
3454
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 address of 'H' too?
13
17195
by: In a little while | last post by:
thanks
3
2159
by: artifact.one | last post by:
Hi. What's the practical difference between: #include <header.h> and: #include "header.h"
13
2509
by: manish sahu | last post by:
difference between calloc() and malloc()
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.