[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
|