Has this C code a defined behavior? -
this thought experiment, not production code nor coding style.
suppose have function
int find_process_pid_by_name(char* name, int* threads_in_process);
that return pid of named process , store threads_in_process
number of threads running in said process.
a lazy programmer, interested on pid, writes code
int pid = find_process_pid_by_name("a process name", &pid);
does trigger undefined behavior?
no — don't think undefined behaviour. there's sequence point before function called (after arguments , expression denotes called function have been evaluated), , there's before called function returns. side-effects on pid
performed called function have been completed before function finishes returning. result of function assigned pid
. there's no question of location assigned being changed function. see nothing invokes undefined behaviour.
i assuming called function treats int *
argument write-only pointer single value. if reads single value, need know pid
initialized (formally; in practice, won't matter). in context, pid
has not been initialized; result of function initialize it. so, if function reads pointer argument, technically, have undefined behaviour. if function treats pointer start of multi-element array , accesses beyond zeroth element, there problems. these issues outside intended scope of question/discussion.
Comments
Post a Comment