2

I have a lib written in C. In code i found a few lines like this int x = x;. I need to rewrite all this pieces of code for compilation with /Zw flag. In some places that mean's int x = some_struct->x;, but in another cases i don't understand what is it. In some places it first use of x variable. So in which cases could be used such int x = x; expression.

void oc_enc_tokenize_dc_frag_list(oc_enc_ctx *_enc,int _pli,
    const ptrdiff_t *_coded_fragis,ptrdiff_t _ncoded_fragis,
    int _prev_ndct_tokens1,int _prev_eob_run1){
    const ogg_int16_t *frag_dc;
    ptrdiff_t          fragii;
    unsigned char     *dct_tokens0;
    unsigned char     *dct_tokens1;
    ogg_uint16_t      *extra_bits0;
    ogg_uint16_t      *extra_bits1;
    ptrdiff_t          ti0;
    ptrdiff_t          ti1r;
    ptrdiff_t          ti1w;
    int                eob_run0;
    int                eob_run1;
    int                neobs1;
    int                token;
    int                eb;
    int                token1=token1;
    int                eb1=eb1;
    /*Return immediately if there are no coded fragments; otherwise we'd flush
       any trailing EOB run into the AC 1 list and never read it back out.*/
    if(_ncoded_fragis<=0)return;
    frag_dc=_enc->frag_dc;
    dct_tokens0=_enc->dct_tokens[_pli][0];
    dct_tokens1=_enc->dct_tokens[_pli][1];
    extra_bits0=_enc->extra_bits[_pli][0];
    extra_bits1=_enc->extra_bits[_pli][1];
    ti0=_enc->ndct_tokens[_pli][0];
    ti1w=ti1r=_prev_ndct_tokens1;
    eob_run0=_enc->eob_run[_pli][0];
    /*Flush any trailing EOB run for the 1st AC coefficient.
      This is needed to allow us to track tokens to the end of the list.*/
    eob_run1=_enc->eob_run[_pli][1];
    if(eob_run1>0)oc_enc_eob_log(_enc,_pli,1,eob_run1);
    /*If there was an active EOB run at the start of the 1st AC stack, read it
       in and decode it.*/
    if(_prev_eob_run1>0){
      token1=dct_tokens1[ti1r];
      eb1=extra_bits1[ti1r];
      ti1r++;
    eob_run1=oc_decode_eob_token(token1,eb1);

code exaple - variable token1 - it's first use of token1 in file and token1 never meets in other files, it's not global, not static anywhere...

Update
with /Zw flag:error C4700: uninitialized local variable 'token1' used
without flag: all works fine with this lib
Update 2
it's theora 1.1.1 lib
Resume
on advice of the guys in comments, i replace every int x = x; with int x = 0 and everything works fine =) everyone thanx for answers

hoody
  • 343
  • 3
  • 12

2 Answers2

7

If you literally have int x = x;, there isn't much use of it. This piece attempts to initialize x with itself, that is, with the value of an uninitialized variable.

This may suppress some compiler warnings/errors related to uninitialized or unused variables. But some compilers can catch these dubious cases as well.

This probably also results in undefined behavior from the C standard's view point.

EDIT: Random Number Bug in Debian Linux is an article (with further links) about use and abuse of uninitialized variables and the price one may pay one day.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • if its global then it might not be undefined – Koushik Shetty Apr 25 '13 at 10:53
  • @Koushik You can't initialize a global variable with itself, only with a constant expression. – Alexey Frunze Apr 25 '13 at 10:54
  • oh maybe its c++ i'm thinking of sorry. – Koushik Shetty Apr 25 '13 at 10:55
  • @Koushik C++ is different. In C++, some of your code (constructors of global/static objects) gets to run before `main()`. – Alexey Frunze Apr 25 '13 at 10:55
  • 1
    @Koushik It would be compilable code, but I'm not so sure about it having defined behavior. – Alexey Frunze Apr 25 '13 at 10:57
  • for C++ globals its defined!:-) [link](http://stackoverflow.com/a/3309123/1825795). even the standard has an example in **3.3.2 Point of declaration**. its clearer in C++ standards than in C. – Koushik Shetty Apr 25 '13 at 11:22
  • @Koushik Thanks, I didn't know that bit about C++. – Alexey Frunze Apr 25 '13 at 11:24
  • in C if they would have mentioned about Point of declaration then clearly then we could have included that in your answer.. – Koushik Shetty Apr 25 '13 at 11:26
  • @Koushik There are no global variables in C. There are four kinds of scopes: function, file, block, and function prototype. An object has a storage duration that determines its lifetime. There are four storage durations: static, thread, automatic, and allocated. You're referring to objects with static storage duration, declared at file scope. – autistic Apr 25 '13 at 11:29
  • @undefinedbehaviour yes i will correct that about myself. thank you. allocated is one that we obtain from free store? – Koushik Shetty Apr 25 '13 at 11:34
  • 1
    @AlexeyFrunze `int x = x;` is well defined in C++ (and even uses it in an example as others have stated). However, as you said it can lead to undefined behaviour in C. Standard quotes: *"The initial value of the object is indeterminate."* and from *definitions* an "indeterminite value" is "either an unspecified value or a trap representation". Using a trap representation is undefined behaviour. – autistic Apr 25 '13 at 11:35
  • @Koushik Allocated describes the storage duration of objects pointed to by the return value of *malloc*, *calloc* and *realloc*. – autistic Apr 25 '13 at 11:38
  • @undefinedbehaviour **"The initial value of the object is indeterminate."** is only for object automatic storage. then standard goes on to say in 6.7.9/10 about If an object that has static or thread storage duration is not initialized explicitly, then:"if it has arithmetic type, it is initialized to (positive or unsigned) zero;" i omitted the rest – Koushik Shetty Apr 25 '13 at 11:39
  • @Koushik Correct. Objects with static storage duration are implicitly initialised to 0, unless they are explicitly initialised to some other constant expression. However this isn't relevant to the question (or the answer), because `x` isn't a constant expression. Thus, `int x = x;` at file scope, or `static int x = x;` would probably result in a constraint violation ("compiler error"). – autistic Apr 25 '13 at 11:41
  • So can I replace `int x = x;` with `int x;` everywere and go for a walk? :) – hoody Apr 25 '13 at 11:44
  • @undefinedbehaviour your name is like an answer to my question )) – hoody Apr 25 '13 at 11:46
  • 1
    @hoody you will be better with `int x = 0` and go watch a movie :-) – Koushik Shetty Apr 25 '13 at 11:46
  • @hoody Yes. You'd want to avoid me, unless you're happy relying upon coincidence. :) – autistic Apr 25 '13 at 12:03
5

It prevents the compiler from emitting a warning that the variable is unused.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179