Memory allocation

In my application,in one function I need total 24 arrays each of size
100000.If I will allocate memory for each array at the start of the fuction using new and will deallocate while exiting the fuction using
delete.Then will there be any problem in future considering memory or
running the application many many times?
 Is there any limit for allocating memory in such a way or there is no limit ?
 I agree process will slow down but is it a proper way?
devayaniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cdesignerCommented:
ma ybe you need to allocate memory on start application and remove at exit from application?
0
shaigCommented:
I agree, it is not a good practice to allocated big blocks of memory frequently, espeshely if they are used frequently.

The limit is te size of your VIRTUAL memory. Check out your task manager to learn more about it (if you are running NT or 2000).

0
devayaniAuthor Commented:
I am working on 98,then what should ne virtual memory for my requirements?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

cyberfrankCommented:
Hi!

If you allocate memory dinamically with new operator, then You have to allways look is it was the memory allocation successfuly. In case that there is no available memory, then the new will return a NULL pointer instead of a valid memory location.
In windows 98 there is also a virtual memory managament. Look for it on the control panel/system dialog. One of the tab pages will have virtual memory button - as far as i remember -.

CF
0
dsandfordCommented:
A problem I can see is allocating this memory at the start of the function, allocating more (smaller) blocks in the middle and then freeing the big blocks at the end.  This could lead to eventual memory fragmentation problems.  Otherwise, it should be OK, as it is only a total of 2.4M, which I do not consider to be a huge amount (if you don't do it TOO often!).

Win '98 Virtual memory?  Right click on 'My Computer', select the 'Performance' tab and click on the 'Virtual Memory' button.

This should default to 'Let Windows manage my virtual memory settings.'.  Windows will increase the swapfile as required.  This is usually OK, but can lead to disk fragmentation, so it is possible to select 'Let me specify my own virtual memory settings.'.  I have set 512M as mine (128M real memory), with minimum and maximum the same, so Windows dosen't try to grow it itself, which can fragment the disk.

Play!

Daz
0
GlennDeanCommented:
> then the new will return a NULL pointer
  new only returns a NULL pointer if you're using the old C++ libraries (for example including iostream.h).  BUT the new C++ libraries (i.e. the ones without the ".h") will instead throw an exception.  
   There is a way to use the new libraries and NOT have operator new throw an exception as follows:

class MyClass
{...}
MyClass * pClass = new(nothrow) MyClass;

On a limit to how much memory you can allocate, your app has 2GiG of space (the other 2GiG is reserve for the OS).  So allocating 24*100000 is not even remotely close to the limit.  And, if you run multiple instances simultaneously of your app this won't effect the other instances (they each have their own virtual 4GiG address spaces).
    Glenn
0
mikeblasCommented:
devayani> In my application,in one function I need
 devayani> total 24 arrays each of size 100000.

You need all 24 arrays all the time? Or do you only need some of the 24 each time this function is called? Is 100000 the size in bytes, or the count of elements? What type is each element in the array?

To answer your question about your virtual memory requirements, you'll need the above answers. Just multiply it out; 24 arrays times 100000 bytes is 2.4 megabytes of memory.  If you are actually allocating some other data, you also need to multiply by the size of that type. For instance, sizeof(double) is 8 bytes; 8 times 24 times 100000 elements is 19.2 megabytes!

 devayani> If I will allocate memory for each array at the start of the
 devayani> fuction using new and will deallocate while exiting the fuction
 devayani> using delete.

Sure, that's the straightforward way.

But you might consider the access pattern of your function. If you call this function very frequently, you'll be better off allocating the memory once and letting the function reuse the arrays. You'll might allocate a buffer before you call the function several times, then free it after that burst of activity.  In pseudocode:

   p = allocate 24 arrays
   for (n = 0; n < SEVERAL_TIMES; n++)
   {
      YourBigFunction(p);
      UseResults(p);
   }
   deallocate p

The general idea is to optimize out repeated reallocation (and frees) of the memory.

 devayani> Then will there be any problem in future considering
 devayani> memory or running the application many many times?

No, as long as you free the memory you've allocated.  If you take my optimization advice to the point of keeping the memory for the duration of the application, you may find that you're sitting on so much memory that _other_ programs starve.

 devayani> Is there any limit for allocating memory in such a way or
 devayani> there is no limit ?

You're limited by available virtual memory. That toal depends on the physical memory in the machine, the settings in control panel, and the memory usage of other apps running on the system at that moment.

 devayani> I agree process will slow down but is it a proper way?

If you truly need that much memory, the proper way is the careful way. You might study your algorithm again to be completely certain that you really need that much memory, especially concurrently.

 cyberfrank> In case that there is no available memory, then
 cyberfrank> the new will return a NULL pointer instead of a
 cyberfrank> valid memory location

That depends--new may throw an exception instead of returning NULL.

..B ekiM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GlennDeanCommented:
You can control how much virtual memory you can have, as Mike indicated, via the Control Panel, System icon, Performance tab, click Virtual Memory button.
0
mikeblasCommented:


 GlennDean>  new only returns a NULL pointer if you're using
 GlennDean> the old C++ libraries (for example including iostream.h).

No. Since this is the MFC topic, we have to assume the questioner is using MFC. MFC provides operator new regardless of the headers you use.

MFC's implementation turns around and calls the C runtime operator new implementation. That implementation will return NULL on failure only if there is no registered new handler. Please see the documentation for the _set_new_handler() and _query_new_handler() functions.

..B ekiM
0
mikeblasCommented:
A "B"?  What more information or advice did you need? What else did you expect me to do?

..B ekiM
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.