Allocating memory can be an expensive operation, especially if memory is fragmented. On low-memory machines, asking for 256MB of contigious RAM might involve a lot of paging. Freeing memory instructs the memory manager to access pages previously put on disk. That might be where the latency comes from.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
Do you really need 256 MB?
Can you break up your data or structure into sub-elements?
You might also try VirtualAlloc instead of using malloc and free.
VirtualAlloc has some options that malloc does not give you.
For what complier/C library? malloc/free is not provided by any OS I'm aware of. Instead your C library (which might come with the compiler, might be included) provides it.
In some Unix systems (and I suspect Windows as well), the library asks the OS for more memory than you requested, and then holds that in a buffer. If you malloc more, it will give you memroy from that same buffer if there is room. When you free the memory it marks it unused, but doesn't return it to the OS, instead it will hold onto it in case you need more memory latter.
On many systems the OS will only allow you to allocate 4Kb chunks (this varies depending on both the OS and the design of the hardware). The library is designed so that you can malloc a few bytes, without getting a full 4kb.
Also, in many cases the library will write something to most of that memory when it is allocated. The OS however may just mark your application as allowed to use that much memory, but not actually give it to you, until you use it.
In effect it is optimized for small accesses, and you are sending it a large acccess which it isn't designed for.
If you don't need portability you might be able to speed things up by using OS specific functions to get the memory from the OS yourself.
Note that none of this will help if you don't have enough physical memory. If you want to use 256M of memory you should have at least 400M of physical memory so the OS doesn't have page everything else out.
I'm creating a wav player and am making use of PlaySound(). Can u please recommend whether to use MCI or continue with the same.. also i need to create a simple bar visualization like the one in winamp.. THANKS!
I've seen some articles here on design patterns, but nothing that looks down to my speed. I would like to learn about design patterns, but actually know nothing about them. Does anyone have any suggestions on where I could start?
Sorry, I think you misunderstood my question. I'm not talking about graphics. I mean design patterns as in 'singleton' pattern; applying patterns to frequently encountered programming problems or situations, if I understand the concept correctly.
[A brave person isn't always necessarily a smart person]