By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,365 Members | 2,946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,365 IT Pros & Developers. It's quick & easy.

What's the benefit to dynamically allocate memory?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.