This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Pointer with a NULL value is not evaluated correctly

Other Parts Discussed in Thread: MSP430FR4133

Hello,

I am implementing a data structure using struct defined in a header file. The definition of structure is as follows:

struct node{

unsigned char data;

struct node *next;

}

I have defined a struct pointer as a global variable:

struct node *REC_q;

REC_q = NULL;

This pointer is operated upon by several functions in the main code which reference/ dereference the pointer depending upon presence of data in the data structure.

I am using a isEmpty function to evaluate if this pointer is NULL as follows:

unsigned int isEmpty(struct node *temp) {

if(temp == NULL) {

return 1;

}

else{

return 0;

}

}

Even if the value of pointer is 0x0000, the isEmpty function returns 0. I am single stepping the code and getting this result.

Note: I am using MSP430FR4133 launchpad with CCS v6.1 (OS is Win7)

Please help me with this. I think I am missing volatile keyword somewhere/ something similar.

Thanks.

  • Do you ever access the list from an interrupt handler? Then you must indeed declare REC_q and next as volatile (and probably other variables, too):

    struct node * volatile next;
    
    struct node * volatile REC_q;
  • Hi Clemens,

    Yes. There is one function in an interrupt handler which modifies the list. I shall try declaring the structure members as volatile.

    However, I have three questions here. There are three scenarios.

    1. I can declare volatile in the header file where the structure is defined as follows:
    struct node{
    volatile unsigned char data;
    struct node * volatile next;
    }
    So does this make all the subsequent structure pointer definitions as volatile?

    2. I can declare volatile where the main node is defined globally:
    struct node * volatile REC_q;

    However, the problem with this is when I pass this as an argument to a function (void operate(&REC_q) which is defined as void operate(struct node **head)) which is internally using free(); function to free dynamically allocated memory, it gives a warning that argument of free() must not be of type volatile.
    What is a solution to this?

    3. I can declare volatile where the local variables are using the char data. Should this solve the problem?

    Appreciate your help.

    Thanks,
    Rohan
  • Please note that "volatile" does not directly affect the data; it changes how the compiler implements accesses to the data.

    1. Declaring all strucure members as volatile makes all accesses to these member variables safe. However, it does not affect any other variable (such as REC_q).
    2. If the operate() function can run at the same time as the interrupt handler, it must be written to handle concurrent accesses correctly, which implies that its node pointer must be volatile (among other things.)
      When you are calling free(), the memory must no be accessible by an interrupt handler (otherwise, the handler might access already-freed memory), so it is safe to cast the volatile away.
    3. A local variable would not be accessed by an interrupt handler. However, if you have a local pointer that points to data that is accessed by an interrupt handler, the pointed-to data must be volatile.

**Attention** This is a public forum