R
Ron Eggler
Hi,
I got a problem: I have a function that allocates new memory to hold another
element of my struct array if it's not existing already.
And this is what happened (copy/paste from my syslog - where prs_log() is
writing to):
"CheckStruct() - Added new element[2]: BusID:T9998 seqNr:9 - Count: 3"
"ClearElement() - Will remove element [0]: BusID:T1111 seqNr:627 - Count: 2"
"CheckStruct() - Added new element[2]: BusID:T9998 seqNr:9 - Count: 3"
Now... Why did it create a second entry tor T9998, SEQ#9?
Shouldn't it have recognized the one that's there already? Held on position
1 after the element 0 has been cleared or do you see some mistake in my
clearing function?
I get pretty stuck here...
Thanks for everybody's hints and suggestions!
Ron
I got a problem: I have a function that allocates new memory to hold another
element of my struct array if it's not existing already.
Code:
/** My Structures **/
typedef struct {
char BusID[6];
int SeqNr;
unsigned short Msg;
} MsgStruct;
typedef struct{
unsigned char app;
unsigned char pri;
unsigned char act;
int seq;
char id[6];
}io_t;
/* My function */
int CheckStruct(MsgStruct **SumStruct_ptr, io_t *CurrentBus)
{
int i=0;
if (StructCount==0){
/* allocating memory to hold the 1st element */
}
/* loop thru all elements in the struct array and check if
busID and seq# matches with an existing element
if not, create new element */
for (i=0; i < StructCount; i++){
if((strcmp((*SumStruct_ptr)[i].BusID, CurrentBus->id)==0) &&
((*SumStruct_ptr)[i].SeqNr== CurrentBus->seq)){
return i;
} /*if we're at the end of the array and nothing matched - extend
memory*/
else if( i == StructCount - 1){
(*SumStruct_ptr) = realloc((*SumStruct_ptr),
(StructCount+1)*sizeof(MsgStruct ));
StructCount++;
strncpy((*SumStruct_ptr)[i+1].BusID, CurrentBus->id, 6);
(*SumStruct_ptr)[i+1].SeqNr=CurrentBus->seq;
(*SumStruct_ptr)[i+1].Msg=0;
prs_log(LOG_NOTICE,"***CheckStruct() - Added new element[%d]: BusID:%s
seqNr:%d - Count: %d", StructCount-1, (*SumStruct_ptr)[i+1].BusID,
(*SumStruct_ptr)[i+1].SeqNr, StructCount);
return i+1;
}
}
}
MsgStruct *
ClearElement(MsgStruct ** SumStruct_ptr,
int Position)
{
/* Check for invalid 'Position' value */
prs_log(LOG_NOTICE,"***ClearElement() - Will remove element [%d]: BusID
%s seqNr:%d - Count: %d", Position,(*SumStruct_ptr)[Position].BusID,
(*SumStruct_ptr)[Position].SeqNr, StructCount);
if ( Position>= StructCount )
return (*SumStruct_ptr);
/* If 'Position' doesn't index the very last element move all the
ones following it to a position with an index smaller by 1,
thereby overwriting the element at 'Position' */
if ( Position != StructCount - 1 )
memmove( SumStruct_ptr + Position, SumStruct_ptr + Position + 1,
( StructCount - Position - 1 ) * sizeof *SumStruct_ptr );
/* The last element isn't used anymore, so give back the memory
used for it. */
StructCount--;
return (*SumStruct_ptr) = realloc( (*SumStruct_ptr), StructCount *
sizeof *SumStruct_ptr );
writing to):
"CheckStruct() - Added new element[2]: BusID:T9998 seqNr:9 - Count: 3"
"ClearElement() - Will remove element [0]: BusID:T1111 seqNr:627 - Count: 2"
"CheckStruct() - Added new element[2]: BusID:T9998 seqNr:9 - Count: 3"
Now... Why did it create a second entry tor T9998, SEQ#9?
Shouldn't it have recognized the one that's there already? Held on position
1 after the element 0 has been cleared or do you see some mistake in my
clearing function?
Thanks for everybody's hints and suggestions!
Ron