pointer confusion - any hints?

R

Ron Eggler

Hi,

I have a struct array in my application. I want to call a function, change
values of struct variables and return back to main - while still keeping
the information written in the function.
I thought I'd do it as pasted below but my problem is that my thread
terminates whenever i access SumStruct_ptr's elements to read/write values
and i have no clue why, could anyone help please?
Thanks alot!
Code:
typedef struct {
        char * BusID;
        int SeqNr;
        unsigned short Msg;
        } MsgStruct;

int StructCount=0;
int CheckStruct (MsgStruct **SumStruct_ptr, io_t *CurrentBus)
{
prs_log(LOG_CRIT,"***in CheckStruct, sizeof(%d), StructCount:%d",
sizeof(SumStruct_ptr), StructCount);
int i=0;
if (StructCount==0){
prs_log(LOG_CRIT,"***in if, StructCount: %d,", StructCount);
(*SumStruct_ptr) = calloc( StructCount + 1, sizeof( MsgStruct ) );
prs_log(LOG_CRIT,"***sucessfully allocated space: %d",
sizeof((*SumStruct_ptr)));
(*SumStruct_ptr)[StructCount].BusID=CurrentBus->id;
(*SumStruct_ptr)[StructCount].SeqNr=CurrentBus->seq;
(*SumStruct_ptr)[StructCount].Msg=0x00;
StructCount++;
prs_log(LOG_CRIT,"***Values in SumStruct_ptr[%d]: BusID:%s seqNr:%d
Msg:0x%x", StructCount, (*SumStruct_ptr)[StructCount].BusID,
(*SumStruct_ptr)[StructCount].SeqNr, (*SumStruct_ptr)[StructCount]);
}
// some more code to extend the array
}

main(void)
{
static MsgStruct *SumStruct;
Pos = CheckStruct(&SumStruct,_cur_io);
SumStruct[Pos].Msg|=TSP_SERVICE_REQUEST;
}
 
P

Peter Nilsson

Ron Eggler said:
...
typedef struct {
        char * BusID;
        int SeqNr;
        unsigned short Msg;
        } MsgStruct;

int StructCount=0;
int CheckStruct (MsgStruct **SumStruct_ptr, io_t
*CurrentBus)
{
prs_log(LOG_CRIT,"***in CheckStruct, sizeof(%d),
StructCount:%d",sizeof(SumStruct_ptr), StructCount);

%d is not appropriate for a size_t.

BTW, some indentation wouldn't hurt.
int i=0;
if (StructCount==0){

I'm straining (and wondering why I'm bothering), but I can't
see a balancing }.
prs_log(LOG_CRIT,"***in if, StructCount: %d,", StructCount);
(*SumStruct_ptr) = calloc( StructCount + 1, sizeof( MsgStruct ) );

Here you allocate 1 MsgStruct. It's simpler to just write

*SumStruct_ptr = malloc(sizeof *SumStruct_ptr);
(*SumStruct_ptr)[0] = 0;

Realise that all bits zero needn't yield a null pointer.
prs_log(LOG_CRIT,"***sucessfully allocated space: %d",
sizeof((*SumStruct_ptr)));
(*SumStruct_ptr)[StructCount].BusID=CurrentBus->id;
(*SumStruct_ptr)[StructCount].SeqNr=CurrentBus->seq;
(*SumStruct_ptr)[StructCount].Msg=0x00;

Here you access [0] which is fine.
StructCount++;

StructCount is now 1.
prs_log(LOG_CRIT,"***Values in SumStruct_ptr[%d]: BusID:%s seqNr:%d
Msg:0x%x", StructCount, (*SumStruct_ptr)[StructCount].BusID,
(*SumStruct_ptr)[StructCount].SeqNr, (*SumStruct_ptr)[StructCount]);}

Here you access [1], which is isn't fine.
// some more code to extend the array

"No lieutenant, your men are already dead."
}

main(void)

Implicit int was deprecated by C90 and removed in C99.

int main(void)
{
static MsgStruct *SumStruct;
Pos = CheckStruct(&SumStruct,_cur_io);
SumStruct[Pos].Msg|=TSP_SERVICE_REQUEST;}

[/Code]
 
B

Barry Schwarz

Hi,

I have a struct array in my application. I want to call a function, change
values of struct variables and return back to main - while still keeping
the information written in the function.
I thought I'd do it as pasted below but my problem is that my thread
terminates whenever i access SumStruct_ptr's elements to read/write values
and i have no clue why, could anyone help please?

snip 30+ lines of horribly unindented code.

Your code still invokes the same undefined behavior it did when you
posted a previous version in alt.comp.lang.c. You only corrected half
the errors pointed out then and I doubt if many will continue to read
the eye test you present here.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top