LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can we load and unload the files in the run time?

Solved!
Go to solution

Can we load and unload the files in the run time?

 

For example there are four files named "test1.h & test1.c" and another set "test2.h & test2.c" (I attached them as attachment to this post).

 

test1.h contains code:

int variable; //variable declared as integer

 

test1.c contains code:

variable = 1; //variable assigned a value

 

test1.h contains code:

char *variable; //variable declared as string

 

test1.c contains code:

variable = "EXAMPLE"; //variable assigned a string

 

 

So here, in this case can I dynamically load / unload the first & second group of files so that the same variable name "variable" can be used both as integer and string? And if yes, how is that to be done?

0 Kudos
Message 1 of 8
(3,839 Views)

What do you mean by "dynamically"?

 

If you want to have a variable that either is an int or a char in the same program run, I'm afraid your only option is to define it as a variant and assign from time to time the proper data type in the variant according to some condition. Next, every time you access the variable you must firstly check which data type is stored in it, next access it in the proper way.

 

If on the other hand your option or to have a run in which the variable is an int, next you stop the program and in a following run it is a char, you may have it by using some appropriade preprocessor clause:

 

#ifdef  CHAR_TYPE

#include "test1.h";        // variable defined as a char

#else

#include "test2.h";        // variable defined as int

#endif

 

Next, every time you want to access the variable you must proceed in the same vay:

 

#ifdef  CHAR_TYPE

  variable = "string";

#else

  variable = 1;

#endif

 

Does it worth the effort?

 

Additionally, keep in mind that this "dynamical" approach can work only in the IDE, where you can properly #define your CHAR_TYPE or not depending on your wishes: when you compile the program, it will have only one #include depending on the definition of the macro.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 8
(3,814 Views)

Roberto,

 

Thanks for a nice reply.

 

But in my case, I need to have one header file where in lot of variables are declared. For Example:

Int test1;

Char *test2;

...

...

 

And then in different files I need to assign data to all these variables. For example:

In file1.c:

test1 = 1;

test2 = "ABC";

...

...

 

In file2.c:

test1 = 2;

test2 = "DEF";

...

...

 

etc.,

 

Then during the run time (not compilation time), first I need to use these values assigned in the file1.c, then after that come to file2.c and use the values given there, and so on...

 

Is there a way to do this in the above stated way?

0 Kudos
Message 3 of 8
(3,803 Views)

I don't understant where exactly the problem is.

 

In your first post you asked for a way to have a variable that is firstly declared as int and then as a char * with the same variable name: this is difficult to accomplish unless you use a variant which can be assigned data of different type as I already told you.

In your last post you are simply assigning different values to the variables, without changing their types: I see no problems in doing it inside a function called at the beginning of processing the functions in one section of the program.

 

So what?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 8
(3,799 Views)

Roberto,

 

Sorry that I did not make it clear to you. My first post is answered by you. It is closed.

 

Now my second post is another scenario. Where in if I do it the way you said, calling the functions, there will be a lot of data sitting on my RAM. To avoid this is there a way to do it the way I asked, to load and unload the files in the run-time?

0 Kudos
Message 5 of 8
(3,795 Views)
Solution
Accepted by pa1majeti

Hi pa1majeti,

you cannot simply consider that some code is loaded in memory or not: in a compiled application all code is permanently in memory unless you split your application in DLLs that you dynamically load when needed and unload when no more needed.

But as far as I can understand from your description, you are better considering the amount of memory occupied by data (i.e. content of variables) rather than the code. If this is the case, your only option is to dynamically allocate memory for variables and free it after it has been used. Now, while this option is valuable if you are using large arrays of data, it is of little or no effect on single variables.

 

As a last hint, considering the huge memory installed on modern computers, are you sure that you really need this additional work? I mean, dynamic allocation of memory implies some additional precautions in programming: checking for errors on each malloc or calloc function, verifying that a pointer is valid before each usage, freeing pointers before reallocating them to avoid memory leaks and so on, so a balance must always be looked for between memory consideration and difficulty in programming.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 6 of 8
(3,776 Views)

Roberto,

 

I agree on the point that "huge memory is available on modern computers". Also got the answer for my problem that there is no other way other than dynamically allocating memory. But yet to decide on the complexity of the code with its usage (error handling and all).

 

Appreciate the different analysis given by you. It will be helpful.

 

Finally thanks a lot Smiley Happy

 

Pavan

0 Kudos
Message 7 of 8
(3,767 Views)
You're welcome! Smiley Happy


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 8
(3,764 Views)