Re: [Dev-C++] RES: Question about software design
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Chris M. <lor...@gm...> - 2007-11-21 02:01:24
|
Sorry about forgetting to post to the list... that gets me most every time.
André Macário Barros wrote:
> Dear Mr. Miller,
>
> It´s not exactly this.
> Let me show what I said in another way:
> //////////////////////////////////////////
> file1.c
> #include "tasks1.h"
> #include "tasks2.h"
> main()
> {
> int *v1, *v2, **v3, i;
> int value;
>
> AllocateRes(&v1, &v2, &v3);
> for(i=0; i<10000; i++){
> function1(v1, v2);
> function2(v1, v2, v3);
> value = function3(v2);
> }
> printf("and the best answer is %d\n", value);
> FreeRes(&v1, &v2, &v3);
> }
> //////////////////////////////////////////
> task1.c (function definitions of task1)
> void function1(int *v1, int *v2)
> {
> // update values of v1 and v2
> }
> //////////////////////////////////////////
> task2.c (function definitions of task2)
> void function2(int **v3)
> {
> // update values of v3
> }
> int GiveAnswer(int *v2)
> {
> int value;
>
> //blah blah blah code
> return(value);
> }
> //////////////////////////////////////////
> I only need to declare v1 and v3 inside
> the main() scope to allocate memory through
> AllocateRes (and to release after).
> If I move v1 and v3 inside function1 or
> function2, I will need to make 10000 malloc´s
> and free´s.
> The only variables (the real ones) needed
> to be in main() function are "i", "v2" and "value".
If you want to get very technical you could put them on the call stack
by making it main(int arg0, int *v1, int *v2, int **v3, int i), since
the call stack is freed at the end of the program's execution.
> The time I wrote for the list from now, I though
> in something like a static variable which can
> detect the first time the function is called
> and do the malloc. The same procedure could be
> made to detect the last function call to do the
> free.
>
> But do you know any other way?
As far as I know, all function arguments are put on a call stack via the
C/C++ function stack, which frees everything when the function
completes. Overall, you've already done a very good job, since the
memory is allocated once, used many times, and then deallocated.
If you did not allocate that memory outside the loop, yes, you would
have 10,0000 calls to malloc, and 10,000 calls to free.
Creating a special hook function to detect when objects pass out of
scope in this case is quite overkill. These are simple arrays, and
don't need to be watched that closely because they aren't going that far.
If you want to get into some cool stuff, check out boost.org and their
C++ "Smart" pointer, which counts the number of references to an object
and destroys it once the reference count is zero (when it falls out of
scope). However, this is only for systems where multiple classes and
potentially multiple threads are using the same data.
On most modern operating systems memory that is used is freed to the
system at the end of the program's execution, with a few exceptions.
Overall, aside from profiling it and running Valgrind (a memory leak
checker) I can't find anything to do differently. If you're really
worried about performance and memory leaks, just profile and check it!
There are many open-source, free to use profilers and memory leak
checkers which you can use to isolate exactly which parts of your
application chew up the most time, and where the memory is leaking from.
And about that putting everything on the call stack...
I wouldn't really suggest doing that, because it wasn't really designed
for that, however, in the Overly Obfuscated C Code Competition I did see
one bloke use that trick to write a 3D maze renderer without declaring a
single variable outside of the main() function's calling arguments.
It was quite cool. He basically called main() a number of times until
he had the memory he wanted and... you can look it up if you're interested.
You're on the right path, I don't seen any need for change. Perhaps
someone more experienced can find something more.
|