|
|
View previous topic :: View next topic |
Author |
Message |
Ron Guest
|
How to clear an Array of Structures |
Posted: Mon Jun 23, 2003 5:58 am |
|
|
How would you clear an array of structures.
Do you use Calloc(), Free() etc.?
Typedef Struct
{
Int I2CAddr;
Long Serial_number;
Int Options;
Int Param1;
Int Param2;
Short Param3;
} TMODULE_DATA
TMODULE_DATA Module[MAX_MODULES];
Somewhere in my program I want to reset all the Module-data in a clean and efficient way. But I don't know how?
Regards,
Ron
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515467 |
|
|
R.J.Hamlett Guest
|
Re: How to clear an Array of Structures |
Posted: Mon Jun 23, 2003 7:36 am |
|
|
:=How would you clear an array of structures.
:=
:=Do you use Calloc(), Free() etc.?
You could potentially allocate the structures with calloc, and then 'free' them, but at this point, your structures will have disappeared, and need to be reallocated. It would work, but might be more trouble than it is worth.
:=Typedef Struct
:={
:= Int I2CAddr;
:= Long Serial_number;
:= Int Options;
:= Int Param1;
:= Int Param2;
:= Short Param3;
:=} TMODULE_DATA
:=
:=TMODULE_DATA Module[MAX_MODULES];
:=
:=Somewhere in my program I want to reset all the Module-data in a clean and efficient way. But I don't know how?
:=
:=Regards,
:=Ron
The simplest way, is that there is a shortcut in the CCS C, which allows you to clear an entire structure, using 'structure_name=0'.
So in your case, with the array of structures:
int count;
for (count=0;count
Module[count]=0;
will result in all the structures being cleared.
Remember also, that you can clear an object using the pointer to it, so:
void mem_clear(char * pointer, int size) {
int count;
for (count=0;count
*(pointer++)=0;
}
can then be called with:
mem_clear(Module, sizeof(Module));
to clear these structures, but will also work for just one element, or any other variable.
The other solution, if you want a 'non zero' clear, would be to declare a constant structure, with the required initialisation values 'blank', and then you can use:
Module[n]=blank;
To clear the structure to a preset value.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515469 |
|
|
Ron Guest
|
Re: How to clear an Array of Structures |
Posted: Tue Jun 24, 2003 8:40 am |
|
|
RJ,
Thanks for your suggestions. The last one seems the best to me.
As a matter of fact, I finally found out that it is already made available by CCS by means of the function Memset()!
Thanks anyway ;-)
Ron
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515520 |
|
|
R.J.Hamlett Guest
|
Re: How to clear an Array of Structures |
Posted: Tue Jun 24, 2003 11:00 am |
|
|
:=RJ,
:=
:=Thanks for your suggestions. The last one seems the best to me.
:=
:=As a matter of fact, I finally found out that it is already made available by CCS by means of the function Memset()!
:=
:=Thanks anyway ;-)
:=
:=Ron
Yes, with a value of '0'.
I have tended to 'steer clear' of the CCS block memory functions, after having problems with memcpy a while ago, on the 18fxx2 family I ended up writing this version:
#byte POSTINC0 = 0xFEE
#byte POSTINC1 = 0xFE6
#byte FSR0H = 0xFEA
#byte FSR0L = 0xFE9
#byte FSR1H = 0xFE2
#byte FSR1L = 0xFE1
#bit GIE = 0xFF2.7
//Replacement for the memcpy routine, including interrupt protection
#inline
void mymemcpy(char *dest,char *source, int num)
{
if (GIE)
{
disable_interrupts(GLOBAL);
#ASM
MOVFF dest,FSR0L
MOVFF &dest+1,FSR0H
MOVFF SOURCE,FSR1L
MOVFF &source+1,FSR1H
LOOP:
MOVFF POSTINC1,POSTINC0
DECFSZ num
GOTO LOOP
#ENDASM
enable_interrupts(GLOBAL);
}
else
{
#ASM
MOVFF dest,FSR0L
MOVFF &dest+1,FSR0H
MOVFF SOURCE,FSR1L
MOVFF &source+1,FSR1H
LOOP1:
MOVFF POSTINC1,POSTINC0
DECFSZ num
GOTO LOOP1
#ENDASM
}
}
Which solved my problem, is slightly larger (because I duplicate all the code according to whether interrupts are enabled/disabled, rather than just storing a flag), but is a lot faster on the 18F than their version, and worked when used both inside/outside interrupts. I have therefore tended to avoid their functions, but I expect this problem is now fixed (it was several compiler versions ago...).
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515532 |
|
|
kypec
Joined: 20 Sep 2003 Posts: 54
|
array of structures and how indexes are calculated |
Posted: Mon Dec 01, 2003 9:48 am |
|
|
Hello everybody,
I have recently found a bug in PCH 3.180
Please look at this code:
Code: |
#include <18F452.h>
#device *=16 //use 16 bit pointers
struct mystruct {
int8 field_1[30];
};
struct mystruct struct_n[16],s_temp;
#locate struct_n=0x200 //array of structures spanning 2 RAM banks
#locate s_temp=0x400 //temporary structure
int16 px,py;
void main(void) {
px=1; //pointer to destination
py=15; //pointer to source
struct_n[px]=s_temp; //good handling of FSR pointers
s_temp=struct_n[py]; //good handling of FSR pointers
struct_n[px]=struct_n[py]; //FSR pointers not loaded properly!!!
while (TRUE) {
}
}
|
Recently I've spotted a problem with copying of one
structure to another. My structures are located in an array
ranging from 0 to 15 and the size of structure is 30 bytes.
There is no problem in the assignment of structures as long as the array
index is declared by a constant.
Example:
struct_n[1]=struct_n[5]; //works perfectly OK
Everything is alright even when one index is a variable and another one is
a constant:
Example:
struct_n[px]=struct_n[3]; //works perfectly OK
struct_n[6]=struct_n[py]; //works perfectly OK
However, when I make an assignment with both array elements indexed
by variables then FSR0 and FSR1 pointers are no properly loaded before
the copying itself takes place. One can see during the dissasembly stepping
that although the values for indexes are properly calculated in @MUL1616
routine, the pointers FSR0 and FSR1 receive corrupted values just before
the copying. I suppose the problem could be solved by earlier loading of
FSRx registers -> right after the @MUL1616 has been called and 16-bit value
of index is known the FSRx should be loaded with that value.
I've already sent this bug to CCS tech support and now I'm
waiting impatiently what will be their reaction.
Kypec |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: array of structures and how indexes are calculated |
Posted: Mon Dec 01, 2003 10:05 am |
|
|
kypec wrote: | Hello everybody,
I have recently found a bug in PCH 3.180 |
What would happen if you declared your structures as a multidimitional array? You may find this works for you.
Code: | INT8 Structure[30,16]; |
Another way to avoid confusing the compiler is to use a go between variable.
Code: | x=struct_n[py];
struct_n[px]=x; |
It does not change the fact you found a bug. |
|
|
kypec
Joined: 20 Sep 2003 Posts: 54
|
interim variable |
Posted: Tue Dec 02, 2003 1:08 am |
|
|
hi Neutone,
multidimensional array is not suitable for me for I really
need to use structures which contains various types of vars.
I have used that int8 [30] array just as an example.
So far I can live with that interim variable which is working fine.
kypec |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|