What's the benefit to dynamically allocate memory?

M

Michael

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
 
A

Alan Johnson

Michael said:
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.
 
D

Default User

Michael said:
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
 
M

Michael

Default said:
There is no dynamic memory allocation above.


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
 
R

red floyd

Michael said:
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.
 
M

Michael

red said:
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?
 
A

Alan Johnson

Michael said:
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.
 
H

Howard

Michael said:
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
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top