0

gcc 4.6.2 c89

I am passing a pointer to a function. This pointer will be passed to another function. And depending on the current value, the value will be changed. So in main the value changed will be value changed in the function.

Because I am going through 2 functions. I just wanted to make sure I haven't done anything that would cause undefined behaviour. As this will run on a system with high transcations.

I cannot return the value in the return type, as I need to return TRUE or FALSE so see if the function succeeded or failed.

I have run under valgrind and it reports no errors:

valgrind --leak-check=full --verbose ./ptr

Many thanks for any suggestions,

#include <stdio.h>

#define FALSE 0
#define TRUE (!FALSE)

static int incoming_sdp(int *pcmu_priority);
static int parse_incoming_sdp(int *pcmu_priority);

int main(void)
{
    int pcmu_priority = 10;

    printf("Start program pcmu_priority [ %d ]\n", pcmu_priority);

    if(incoming_sdp(&pcmu_priority) == FALSE) {
        /* Something bad happened */
    }

    printf("Final pcmu priority [ %d ]\n", pcmu_priority);

    return 0;
}

/* Removed the processing that will determine of the function is success of failure */
static int incoming_sdp(int *pcmu_priority)
{
    /* Pass the value of the pcmu_prority */
    if(parse_incoming_sdp(pcmu_priority) == FALSE) {
        return FALSE;
    }

    /* Return true or false if processing was successfull */
    return TRUE;
}

/* Removed the processing that will determine of the function is success of failure */
static int parse_incoming_sdp(int *pcmu_priority)
{
    /* Now change the value of the pcum_priority */
    if(*pcmu_priority == 10) {
        *pcmu_priority = 20;
        printf("pcmu is 10 changing pcmu_priority to [ %d ]\n", *pcmu_priority);
    }
    else if(*pcmu_priority == 20) {
        *pcmu_priority = 10;
        printf("pcmu is 20 changing pcmu_priority to [ %d ]\n", *pcmu_priority);
    }

    /* Return true or false if processing was successfull */
    return TRUE;
}
4
  • This would probably be better posted on codereview Commented Feb 5, 2012 at 10:38
  • What exactly are you asking? If the code has any possibility under some compiler/extreme conditions to cause undefined behaviour? If so I can't think of something by skimming over the code. Just a small note in parse_incoming_sdp() function there is no way to actually return FALSE under you current method. Is this intended? Commented Feb 5, 2012 at 10:49
  • In the code snippet I posted. I have removed all unrelated code. So the code snippet is correct. The full version would do some processing based on the values. Thanks. Commented Feb 5, 2012 at 10:51
  • Note that you don't change the value of the pointer itself; you change the value of the variable that the pointer points at. This is a big difference. You'd be changing the pointer if you wrote pcmu_priority++ in one of the functions (not main). If you then dereferenced the changed pointer, then you would be invoking undefined behaviour. As it is, you're fine. Commented Feb 5, 2012 at 16:15

1 Answer 1

1

This program is fine and does not produce an output that depends on undefined behavior.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.