pointer confusion - any hints?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Eggler

    pointer confusion - any hints?

    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;
    }
    --
    weeks of software enineering safe hours of planing ;)
  • Peter Nilsson

    #2
    Re: pointer confusion - any hints?

    Ron Eggler <unkn...@exampl e.comwrote:
    ...
    typedef struct {
            char * BusID;
            int SeqNr;
            unsigned short Msg;
            } MsgStruct;
    >
    int StructCount=0;
    int CheckStruct (MsgStruct **SumStruct_ptr , io_t
    *CurrentBus)
    {
    prs_log(LOG_CRI T,"***in CheckStruct, sizeof(%d),
    StructCount:%d" ,sizeof(SumStru ct_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_CRI T,"***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_CRI T,"***sucessful ly allocated space: %d",
    sizeof((*SumStr uct_ptr)));
    (*SumStruct_ptr )[StructCount].BusID=CurrentB us->id;
    (*SumStruct_ptr )[StructCount].SeqNr=CurrentB us->seq;
    (*SumStruct_ptr )[StructCount].Msg=0x00;
    Here you access [0] which is fine.
    StructCount++;
    StructCount is now 1.
    prs_log(LOG_CRI T,"***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(&Su mStruct,_cur_io );
    SumStruct[Pos].Msg|=TSP_SERVI CE_REQUEST;}
    >
    [/Code]
    --
    Peter

    Comment

    • Barry Schwarz

      #3
      Re: pointer confusion - any hints?

      On Tue, 03 Jun 2008 18:29:14 GMT, Ron Eggler <unknown@exampl e.com>
      wrote:
      >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

      Comment

      Working...