473,769 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Allocating memory correctly

Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?

thanks!
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #1
10 1861
Hi,
"Jakob Bieling" <ne*****@gmy.ne t> wrote in message
news:bq******** *****@news.t-online.com...
Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?
Typically new has to search for a free block that is large enough. Automatic
variables are usually faster (because it is just some space on the stack).
Try to use variables on the stack as much as possible. For variables that
take up a lot of space or that you just have to create dynamically use
dynamic allocation.

If it slows down your program you can always overload the new operator and
make a smarter (and likely more wasteful considering memory) implementation.

Regards, Ron AF Greve.
thanks!
--
jb

(replace y with x if you want to reply by e-mail)

Jul 22 '05 #2
"Moonlit" <al******@jupit er.universe> wrote in message
news:3f******** *************@n ews.xs4all.nl.. .
Whenever allocating memory using operator new or operator new[] I feel like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?

Typically new has to search for a free block that is large enough.

Automatic variables are usually faster (because it is just some space on the stack).
Try to use variables on the stack as much as possible. For variables that
take up a lot of space or that you just have to create dynamically use
dynamic allocation.


Hi,

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would. But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself or if using many vector's helps (not sure
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?) or if everything is fine the way I have
it.

thanks
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #3
Hi,

"Jakob Bieling" <ne*****@gmy.ne t> wrote in message
news:bq******** *****@news.t-online.com...
"Moonlit" <al******@jupit er.universe> wrote in message
news:3f******** *************@n ews.xs4all.nl.. .
Whenever allocating memory using operator new or operator new[] I feel like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?

Typically new has to search for a free block that is large enough.

Automatic
variables are usually faster (because it is just some space on the stack). Try to use variables on the stack as much as possible. For variables that take up a lot of space or that you just have to create dynamically use
dynamic allocation.


Hi,

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would. But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself or if using many vector's helps (not sure


I wouldn't start with that.
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?) or if everything is fine the way I have it.
If I where you I would use vector<> for arrays.

Then later on when your app actually does run too slow and an analysis of
your code shows it is due to new/delete you might invest some time in
overloading new/delete operator (do not release the memory for the class but
insert it into a linked list then when a new is done return the top of the
list).

Regards, Ron AF Greve.

thanks
--
jb

(replace y with x if you want to reply by e-mail)

Jul 22 '05 #4
Jakob Bieling wrote:

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would.
Those are good reasons to use the free store.
But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself
If you can write a memory manager that's better than the one that comes
with your compiler, by all means do it. <g> One advantage that you might
have is that you know the details of your application's memory usage. If
so, you might be able to do a better job for your application than the
general-purpose memory manager that comes with your compiler. But be
warned: memory managers are hard to write, and even harder to debug,
because the symptoms of a bug usually show up far from the point where
the actual error occurs.
or if using many vector's helps (not sure
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?)
Maybe. But don't underestimate the capabilities of the memory manager
that comes with the compiler. They're often quite good.
or if everything is fine the way I have it.


If your application doesn't meet its requirements and you've determined
that the cause is the memory manager then you have to replace the memory
manager. If you application meets its requirements then everything is
fine the way you have it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #5
"Pete Becker" <pe********@acm .org> wrote in message
news:3F******** *******@acm.org ...
Jakob Bieling wrote:

in my case I cannot use automatic variables, since the allocations are either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would.


Those are good reasons to use the free store.
But since a lot of
those allocations might be done, I am asking myself if it is necessary to write a memory manager myself


If you can write a memory manager that's better than the one that comes
with your compiler, by all means do it. <g> One advantage that you might
have is that you know the details of your application's memory usage. If
so, you might be able to do a better job for your application than the
general-purpose memory manager that comes with your compiler. But be
warned: memory managers are hard to write, and even harder to debug,
because the symptoms of a bug usually show up far from the point where
the actual error occurs.
or if using many vector's helps (not sure
about its allocator, but I am guessing/hoping that it uses the same memory pool for the same kind of vectors?)


Maybe. But don't underestimate the capabilities of the memory manager
that comes with the compiler. They're often quite good.
or if everything is fine the way I have it.


If your application doesn't meet its requirements and you've determined
that the cause is the memory manager then you have to replace the memory
manager. If you application meets its requirements then everything is
fine the way you have it.

Thanks guys, I'll just use the default thingies and see how well it
performs. :)
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #6
On Sun, 7 Dec 2003 12:21:31 +0100, "Jakob Bieling" <ne*****@gmy.ne t>
wrote:
Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?


Yes it is. That's why the STL provides the means to substitute custom
allocators, and often package their own allocators. Typically, they
allocate memory in big chunks, arrays of uninitialized objects that
can be used for more quick and efficient allocation for small objects.
The standard allocator is notably slow performing in multithreading
and multiprocessing situations. STLport, I hear, have a very good
allocator.
Jul 22 '05 #7
"Dan W." wrote:

STLport, I hear, have a very good
allocator.


"Very good" depends on what your requirements are. The STLport allocator
is fast, but it doesn't release memory for use by the rest of the
application.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #8
In general, with C++, you should avoid dynamic memory allocations.
Dynamic memory allocation should be considered only after you have
determined that a local variable or a fixed size allocation will not
do the job.

Also, it might be better to use STL than direct dynamic allocations
in your code. STL will take care of allocating and freeing memory as
needed.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - System Architecture Design CASE Tool
Jul 22 '05 #9
Pete Becker wrote:
"Dan W." wrote:
STLport, I hear, have a very good
allocator.

"Very good" depends on what your requirements are. The STLport allocator
is fast, but it doesn't release memory for use by the rest of the
application.


darn - those nasty trade-offs ... :-)

I suppose it keeps us software engineers in jobs - oh wait a sec, some
engineers in jobs.

Jul 22 '05 #10

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

Similar topics

6
4128
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): // first way CBox x; x.size(); // second way
4
745
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory " statement. This happens for some inputs and not all. What can be the reason for such fault ?
7
2098
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO { struct _NAME_INFO *Next; ULONG LastId; ULONG Id; PVOID Value;
3
5048
by: Tod Birdsall | last post by:
Hi All, The organization I am working for has created a new corporate website that used Microsoft's Pet Shop website as their coding model, and dynamically served up content, but cached each page by content ID. The site appears to be working well. It is hosted on a Windows 2003 server with 2 Gigs of RAM. It was built using Visual Studio .NET 2005 and us running under the .NET Framework 2.0 Beta The Issue :
19
3816
by: allanallansson | last post by:
Hi i would like some guidelines for a experiment of mine. I want to experiment with the swap and ctrl-z in linux. And for this i want to create a c program that allocates almost all the free memory resources. So far i have tried to use #include <stdio.h> int main() { int *ip;
94
4770
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
29
2834
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? Here N is a positive integer, and I am assuming that malloc() succeeds in allocating the chunk of memory specified.
12
2167
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated variable to point to the linked list in main program and that I have to have some kind of a method that initializes the list (plus other methods to manipulate list, of course). "Correctly" means that there should be pointers involved, and possibly...
2
1622
by: Evolution445 | last post by:
Hi, I have a function that reads 17 bytes starting from &H7C2B84. Each time it's about to read it throws an AccessViolationException, which, if I read posts around the net correctly means that I'm reading from protected or unsafe memory, and the solution is to allocate memory. I've been searching for examples on allocating memory, but all of these were for either C++ or C#. I tried something like: Marshal.AllocHGlobal(17) but it...
0
9589
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
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
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...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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
6674
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();...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.