Thread: [Dev-C++] RES: Question about software design
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2007-11-21 01:41:09
|
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".
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?
Thank you!
Andre
-----Mensagem original-----
De: Chris Miller [mailto:lor...@gm...]
Enviada em: terça-feira, 20 de novembro de 2007 22:05
Para: André Macário Barros
Assunto: Re: [Dev-C++] Question about software design
André Macário Barros wrote:
> Dear users,
>
> a) I have a project file, with some .c and .h files;
> b) the main() function is worried in only find one answer,
> which is the best distance (an integer) and the best tour,
> an 1-D vector;
> c)to find this answer, I have to manipulate a lot of 1-D and
> 2-D vectors, and other simple variables in specific functions.
> I´m using parameter passing functions;
> d) the size of these vectors is variable and it depends of the
> user settings. So, I have to use dynamic allocation with pointers;
> e) I´m using two functions: AllocateResources() in the beginning
> of the execution and FreeResources() at the end to do all the
> allocations needed to run my functions. These functions are in
> the main() scope. Because of this, all the vectors need to be
> declared in the main().
>
> My problem is:
> Many of these vectors are only related to specific functions,
> that is, they are only used in some functions and I have to
> make reference to them in the main() scope.
> If I put the allocation inside the functions, I will make
> thousands of malloc´s and free´s for the same thing!
>
> My question is:
> Is there any clever way to do this (make only one allocation,
> run the program, free the resources at the end of the execution
> and DON´T NEED TO EXIBIT ALL THESE RESOURCES-the vectors IN THE
> main() SCOPE)?
What I think you're trying to say is that you have a bunch of variables
declared above your main() function, and that you don't want them to be
there, however, you want to be able to manipulate them in the scope of
main().
Am I right? I don't want to proceed any further without clarifying
because I'm not entirely sure I understand what you're asking.
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.16.1/1140 - Release Date: 19/11/2007
19:05
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.16.1/1140 - Release Date: 19/11/2007
19:05
|
|
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.
|
|
From: Rafael M. <so...@gm...> - 2007-11-21 04:18:31
|
Sorry Chris.... So.... you just don't like to use new and delete, correct? That isn't the ready-to-go way to alloc memory, use for any time and free as you wish, in c++ (I use that all the time. Truly, I don't use static variables anymore, in C and C++.)? (same is true to c with free and calloc). This approach can be very useful if you are not interested in reinvent the wheel (Like proof of concept and other study cases.) -- Regards Rafael Menezes |
|
From: <and...@su...> - 2007-11-21 11:18:48
|
I understood that you are asking about the advances C++ can give us in these aspects, right? My interest is to focus the code only in ANSI C for some reasons. One of them is to put the same code in a C-microcontroler, like PIC16F877, with a minimum level of modification. But, let me make one question related to this: I´ve read here that the free() command isn´t necessary, is it? At the end of the execution of the program all the dynamic memory requested is released automatically. Is that true? Regards Andre -----Mensagem original----- De: Rafael Menezes [mailto:so...@gm...] Enviada em: quarta-feira, 21 de novembro de 2007 01:18 Para: André Macário Barros Cc: Lista do Dev-C++ Assunto: Re: [Dev-C++] RES: Question about software design Sorry Chris.... So.... you just don't like to use new and delete, correct? That isn't the ready-to-go way to alloc memory, use for any time and free as you wish, in c++ (I use that all the time. Truly, I don't use static variables anymore, in C and C++.)? (same is true to c with free and calloc). This approach can be very useful if you are not interested in reinvent the wheel (Like proof of concept and other study cases.) -- Regards Rafael Menezes No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.503 / Virus Database: 269.16.2/1142 - Release Date: 20/11/2007 17:44 |
|
From: Per W. <pw...@ia...> - 2007-11-21 13:05:47
|
The free() before program termination isn't needed when the application is run under any normal operating system. But when talking embedded systems, there is normally no such resource manager available in the OS - if there even is an OS. Cleaning up memory is a good thing to do even when not needed. One reason is that you can use automatic tools to find memory leaks - the allocations that you have forgotten, and that may be located inside some form of loop and result in a program failure after some time because you run out of memory. Without a free() at the end of program, the test will always complain about "lost" memory, and you will always ignore the result of the test - so you will not notice these unexpected leaks. One example of an often forgotten leak is strdup(). /pwm On Wed, 21 Nov 2007, Andr=E9 Mac=E1rio Barros wrote: > I understood that you are asking about the advances C++ can > give us in these aspects, right? > > My interest is to focus the code only in ANSI C for some reasons. > One of them is to put the same code in a C-microcontroler, like > PIC16F877, with a minimum level of modification. > > But, let me make one question related to this: I=B4ve read here that > the free() command isn=B4t necessary, is it? At the end of the execution > of the program all the dynamic memory requested is released > automatically. Is that true? > > Regards > Andre > -----Mensagem original----- > De: Rafael Menezes [mailto:so...@gm...] > Enviada em: quarta-feira, 21 de novembro de 2007 01:18 > Para: Andr=E9 Mac=E1rio Barros > Cc: Lista do Dev-C++ > Assunto: Re: [Dev-C++] RES: Question about software design > > > Sorry Chris.... > > So.... you just don't like to use new and delete, correct? That isn't t= he > ready-to-go way to alloc memory, use for any time and free as you wish, i= n > c++ (I use that all the time. Truly, I don't use static variables anymore= , > in C and C++.)? (same is true to c with free and calloc). This approach c= an > be very useful if you are not interested in reinvent the wheel (Like proo= f > of concept and other study cases.) > > > -- > Regards > Rafael Menezes > No virus found in this outgoing message. > Checked by AVG Free Edition. > Version: 7.5.503 / Virus Database: 269.16.2/1142 - Release Date: 20/11/20= 07 > 17:44 > |