473,326 Members | 2,110 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,326 software developers and data experts.

What's the benefit to dynamically allocate memory?

Hi,

What's the benefit to dynamically allocate memory?

using namespace std;

int main()
{
char* ptr;
ptr="abc";
cout<<ptr;
return 0;
}

Won't the memory be cleared after executing the above code?

Thanks in advance,
Michael

Aug 3 '06 #1
7 2772
Michael wrote:
Hi,

What's the benefit to dynamically allocate memory?

using namespace std;

int main()
{
char* ptr;
ptr="abc";
cout<<ptr;
return 0;
}

Won't the memory be cleared after executing the above code?

Thanks in advance,
Michael
Now write a program that reads into memory an arbitrarily long string
of text from stdin, without using dynamically allocated memory.

--
Alan Johnson

Aug 3 '06 #2
Michael wrote:
Hi,

What's the benefit to dynamically allocate memory?
When you need memory to be allocated dynamically. Do you know what that
means?
using namespace std;

int main()
{
char* ptr;
ptr="abc";
cout<<ptr;
return 0;
}
There is no dynamic memory allocation above.
Won't the memory be cleared after executing the above code?
What memory? No memory was allocated.
Thanks in advance,
Go get yourself a decent textbook.

Brian
Aug 3 '06 #3

Default User wrote:
using namespace std;

int main()
{
char* ptr;
ptr="abc";
cout<<ptr;
return 0;
}

There is no dynamic memory allocation above.
Won't the memory be cleared after executing the above code?

What memory? No memory was allocated.
I am confused.
when ptr="abc" no memory was assinged to the point?

all the memory assignment & delete have to deal with
new or delete ?

Please help me out.

Thanks,
Michael

Aug 4 '06 #4
Michael wrote:
Default User wrote:
>>using namespace std;

int main()
{
char* ptr;
ptr="abc";
cout<<ptr;
return 0;
}
There is no dynamic memory allocation above.
>>Won't the memory be cleared after executing the above code?
What memory? No memory was allocated.

I am confused.
when ptr="abc" no memory was assinged to the point?
No, memory was assigned to the pointer, but it was not dynamically
allocated.

Dynamic memory is allocated with new, and freed up with delete.

However, you generally don't want to use bare pointers, if you can avoid
it. Find a good smart pointer class (Boost has some good ones), and use
that for single objects. Use standard library containers (vector, list,
set, deque, etc...) for when you want to allocate multiple objects. And
even auto_ptr has its uses.

Aug 4 '06 #5

red floyd wrote:
Michael wrote:
Default User wrote:
>using namespace std;

int main()
{
char* ptr;
ptr="abc";
cout<<ptr;
return 0;
}

No, memory was assigned to the pointer, but it was not dynamically
allocated.

Dynamic memory is allocated with new, and freed up with delete.
So when the not-dynamically assinged memory was released? So use
dynamically assigned memory to make sure memeoy is enough to use,
right?

Aug 4 '06 #6
Michael wrote:
Default User wrote:
>>using namespace std;

int main()
{
char* ptr;
ptr="abc";
cout<<ptr;
return 0;
}
There is no dynamic memory allocation above.
>>Won't the memory be cleared after executing the above code?
What memory? No memory was allocated.

I am confused.
when ptr="abc" no memory was assinged to the point?

all the memory assignment & delete have to deal with
new or delete ?

Please help me out.

Thanks,
Michael
C++ defines three "storage durations", static, automatic, and dynamic.

Objects with static storage duration exist for the duration of the
program. String literals, such a "abc" in your example, have static
storage duration.

Objects with automatic storage duration exist until the end of the block
in which they are created. The pointer ptr in your example has
automatic storage duration.

Objects with dynamic storage duration exist between the time they are
created with new, and the time they are destroyed with delete. You do
not have any of those in your example.

Though the standard does not place any requirements on how any of that
is implemented, a typical implementation is that objects with static
storage duration are created as part of the binary image, objects with
automatic storage duration are created on some type of system stack, and
objects with dynamic storage duration exist in some part of memory that
is managed by your libraries or OS and sometimes referred to as "the heap".
So, to answer your question, the line:
ptr = "abc";
does cause the variable ptr (which has automatic storage duration) to
contain the address of the string literal "abc" (which has static
storage duration), but at no time is there any dynamic memory allocation
occurring.

--
Alan Johnson
Aug 4 '06 #7

"Michael" <mi*******@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Hi,

What's the benefit to dynamically allocate memory?
Michael, it seems as if you are trying to learn C++ on your own by asking a
lot of questions here. A couple of good C++ books would provide a much
better source for answers to most of your questions, I think. (I use
Stroustrup's books for references, as well as Meyers' for discussions on a
lot of good C++ programming practices.)

You might also find the FAQ extremely helpful:
http://www.parashift.com/c++-faq-lite/. (I think there are also recommended
books on a link there somewhere.)

There are also tutorials on C++ on the web, including one especially for
newbies by a regular here, Alf P. Steinbach:
http://home.no.net/dubjai/win32cpptut/html/, (but I haven't looked at it
myself).

Finally, a good course in your local school might be benificial. In
addition to giving you the fundamentals, teachers are (or at least can be) a
really good source for addressing these kind of questions in person.

Hope this helps....

-Howard


Aug 4 '06 #8

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

Similar topics

2
by: Ralph Krausse | last post by:
How can I dynamically allocate memory in VB? I wish to do this.... Dim szData as String * 250 I can't allocate memory like this cause I am not sure how much to alloate. I want to do...
3
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
32
by: Conrad Weyns | last post by:
I have recently gone back to doing some work in c, after years of c++, but I find the lack of templates and in particular the container in the stl to be a huge show stopper. There are math libs,...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
8
by: Narf the Mouse | last post by:
I'm currently working on a roguelike as an excercise in learning c++. Anyway, I've set up a script parser to check a text file to find out how many races there are, then dynamically alocate an...
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
14
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.