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

What is the solution..?

P: n/a
Hi,

Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
Regards
Mayank Jain(Nawal)
co***************@gmail.com
9818390836

Aug 20 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
<co***************@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
Hi,

Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
using dynamic allocation of memory. use new
Aug 20 '07 #2

P: n/a
On Aug 20, 9:32 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
<contactmayankj...@gmail.comwrote in message

news:11**********************@i38g2000prf.googlegr oups.com...
Hi,
Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.

using dynamic allocation of memory. use new
How new is going to solve my problem. Are you trying to say use new
instead of malloc for allocating memory?
Regards
Mayank Jain(Nawal)
co***************@gmail.com
9818390836

Aug 20 '07 #3

P: n/a
On 2007-08-20 18:21, co***************@gmail.com wrote:
Hi,

Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
It depends on the situation, there is no perfect solution that will work
in all situations. I think that the advice is rather that one should
prefer automatic storage over dynamic when possible. However, one should
be aware that using automatic storage is not possible in all cases, in
fact most non-trivial applications will make use of dynamic memory,
either directly or indirectly via containers.

For some applications were objects of a certain kind needs to be
allocated and deallocated frequently a pool-allocator might increase the
performance and the effects of fragmentations is lessened.

--
Erik Wikström
Aug 20 '07 #4

P: n/a
<co***************@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
Hi,

Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
Dynamic allocation is the ONLY solution to allocating memory at run time
(unless you use a language that provides garbage collection). If you have
problems with fragmentation then the only thing you can do is smarter
dynamic allocation, which requires knowledge of the allocation requirements
of your application.

Aug 20 '07 #5

P: n/a
On Aug 20, 9:49 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcpwrote:
<contactmayankj...@gmail.comwrote in message

news:11**********************@i38g2000prf.googlegr oups.com...
Hi,
Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.

Dynamic allocation is the ONLY solution to allocating memory at run time
(unless you use a language that provides garbage collection). If you have
problems with fragmentation then the only thing you can do is smarter
dynamic allocation, which requires knowledge of the allocation requirements
of your application.
What are the smarter way of allocating memory. I know the time and all
details about the memory requirement of my application. So how can it
help me in improving the efficiency of my code. Can you write a sample
code for this.
What is pool allocator?
Regards
Mayank Jain(Nawal)
co***************@gmail.com
9818390836

Aug 20 '07 #6

P: n/a
On Aug 20, 11:56 am, "contactmayankj...@gmail.com"
<contactmayankj...@gmail.comwrote:
On Aug 20, 9:49 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-

scottmcpwrote:
<contactmayankj...@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
Hi,
Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
Dynamic allocation is the ONLY solution to allocating memory at run time
(unless you use a language that provides garbage collection). If you have
problems with fragmentation then the only thing you can do is smarter
dynamic allocation, which requires knowledge of the allocation requirements
of your application.

What are the smarter way of allocating memory. I know the time and all
details about the memory requirement of my application. So how can it
help me in improving the efficiency of my code. Can you write a sample
code for this.
What is pool allocator?
Regards
Mayank Jain(Nawal)
contactmayankj...@gmail.com
9818390836- Hide quoted text -

- Show quoted text -

I can answer this for you. Can you put me in touch with your
employer, so he can redirect your paychecks to me as well?

Aug 20 '07 #7

P: n/a
co***************@gmail.com wrote:
Hi,

Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
Why do you think it is a concern? Has this caused problems in your
current project?


Brian
Aug 20 '07 #8

P: n/a
co***************@gmail.com wrote:
:: On Aug 20, 9:32 pm, "Jim Langston" <tazmas...@rocketmail.com>
:: wrote:
::: <contactmayankj...@gmail.comwrote in message
:::
::: news:11**********************@i38g2000prf.googlegr oups.com...
:::
:::: Hi,
:::
:::: Its said that one should avoid dynamic allocation of memory, as
:::: it de- fragment the memory into small chunks and allocation of
:::: memory is a costly process in terms of efficiency. So what is
:::: the best solution to handle to situation in which we need to
:::: allocate memory at run time.
:::
::: using dynamic allocation of memory. use new
::
:: How new is going to solve my problem. Are you trying to say use new
:: instead of malloc for allocating memory?

He is saying that you should use new (and not malloc) to allocate
dynamic memory, if you need to do that. If you don't need dynamic
memory, you shouldn't allocate any.

The meaning of 'avoid' is "don't do it, unless you have to", not
"never do it".
Bo Persson
Aug 20 '07 #9

P: n/a

co***************@gmail.com wrote:
What is pool allocator?
Go to http://www.boost.org. They have some
implementations for pool allocators and explanations
of what it is.

Regards,

Werner

Aug 20 '07 #10

P: n/a
<co***************@gmail.comwrote in message
news:11*********************@i13g2000prf.googlegro ups.com...
What are the smarter way of allocating memory. I know the time and all
details about the memory requirement of my application. So how can it
help me in improving the efficiency of my code.
There are decades of research on this. You could start with a Google search
for "low fragmentation heap"

Aug 20 '07 #11

P: n/a
On Mon, 20 Aug 2007 16:21:51 -0000, "co***************@gmail.com"
<co***************@gmail.comwrote in comp.lang.c++:
Hi,

Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
"It is said..." by whom? What makes you think this mysterious third
person source of information is correct?

If you do think this anonymous source is correct, ask he/she/it for
recommendations.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 21 '07 #12

P: n/a
On Aug 21, 1:49 am, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcpwrote:
Dynamic allocation is the ONLY solution to allocating memory at run time
(unless you use a language that provides garbage collection).
Getting there. I would argue that dynamic allocation IS - by the very
meaning of the word dynamic - allocation at run-time. It's not just
the only solution, it is indivisibly the thing itself. "garbage
collection" only refers to one aspect of the myriad design and
implementation choices for algorithms offering dynamic memory
management.
If you have
problems with fragmentation then the only thing you can do is smarter
dynamic allocation, which requires knowledge of the allocation requirements
of your application.
Precisely.

Mayank: what's good depends a lot on the patterns of memory usage.

There are a couple distinct aspects.

Firstly: there's how often you need to do allocations. For example,
if you are having trouble inserting millions of elements in a
container, then see if there are functions to preallocate the required
space rather than grow during insertions, or at least to resize more
aggressively (e.g. resize arrays or hash tables by x4 instead of x2
each time more space is needed). How much can be done depends on the
container: for example, linked lists aren't likely to allow any kind
of pre-allocation.

Secondly: you can decrease the time needed for the allocations you
really can't avoid. To do this, follow suggestions elsewhere in this
thread, and use a custom allocator from boost.

So, to work out which direction to head down, you should profile your
application and work out which containers are causing you trouble,
during what kind of operations (insert, lookup, erase).

Taking a step back, in your original post:
Its said that one should avoid dynamic allocation of memory, as it de-
fragment the memory into small chunks and allocation of memory is a
costly process in terms of efficiency. So what is the best solution to
handle to situation in which we need to allocate memory at run time.
"It's said" suggests even looking at memory allocation may be a path
you've started on with no clear evidence from actual profiling. Did
someone at work tell you "this things slow, think it's the memory
allocation", and ask you to fix it? Don't believe them unless you see
hard evidence in the form of profiling results: you'll waste a lot of
time.

Cheers,

Tony

Aug 21 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.