Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Hey yesterday the message board didn't work...but thank to your advices I got the solution of my problem.
The fact is that since I started to program in c nobody told me that I could declare global variables... now I declare the arrays outside the main and I got all the memory I need. Thank you very much...bye
Why? I heard that dynamic allocation slows down the program and I need much statistic, so I'd prefer a faster simulation.
That is a myth. Regardless if dynamic or static, the OS still needs to allocate it, and it usually goes to the same system call in the end anyway.
Dynamic memory can be slow if you use is incorrectly. But if you simply need one big block of memory, it should be equal in speed.
i.e. most people who care about performance will operate on their assumptions, which is worse than actually measuring, or in fact, not caring at all.
Memory allocation is not instantaneous, true, but it is not relevant to user time. Ever.
Well said.
The "dynamic memory allocation can slow the program" refers to situations that you have no other choice. For example:
for(alot){
do stuf
re-allocate memory
}
The reallocation may slow a lot the program compared to "do stuff". But there is no other way to achieve this. So you are warned that allocation isn't free.
If you just allocate in the beginning of your program memory then it shouldn't really matter (a lot) where you allocate memory. If the program runs main and allocates memory, or pre-allocates memory for globals and afterward calls main should make small difference
You can make dynamic allocation very slow, if you want to. If you want it fast, then don't do that. (I.e., calling malloc once in the program should not slow your program down to any appreciable degree. Calling malloc once for every element in your array will make your program very slow indeed.)
> Why? I heard that dynamic allocation slows down the program and I need much statistic, so I'd prefer a faster simulation.
And using global memory for such a thing is stupid. Take your pick, "slow" or stupid?
Well depending on how you do it, there may be no slow down or close to none (~0%) [a].
[a] For example grow in chunks of 1 MB as you require memory -- or 10MB
Last edited by zacs7; 12-22-2008 at 06:33 AM.
Ok... but there's any problem in using global arrays?
(Sorry but my basic knowledge is very poor)
There would be the problems associated with global variables in general (we have discussed this in recents threads, so you might want to do a search).Originally Posted by p3rry
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)