【问题标题】:What could be case of use `int x = x;` expression (C language)?使用`int x = x;`表达式(C语言)可能是什么情况?
【发布时间】:2013-04-25 10:47:45
【问题描述】:

我有一个用 C 编写的库。在代码中,我发现了几行类似 int x = x; 的代码。我需要重写所有这些代码以使用 /Zw 标志进行编译。在某些地方,这意味着int x = some_struct->x;,但在另一些情况下,我不明白它是什么。在某些地方它首先使用 x 变量。那么在哪些情况下可以使用int x = x;这样的表达式。

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);

代码示例 - 变量 token1 - 这是第一次在文件中使用 token1token1 从未在其他文件中遇到过,它不是全局的,也不是任何地方的静态...

更新
使用 /Zw 标志:错误 C4700:使用了未初始化的局部变量 'token1'
没有标志:这个库一切正常
更新 2
这是theora 1.1.1 lib
简历
根据 cmets 人员的建议,我将每个 int x = x; 替换为 int x = 0 并且一切正常 =) 每个人都感谢答案

【问题讨论】:

  • 你把我弄丢了。你需要更清楚你正在尝试做什么。还有一些代码示例会有所帮助
  • 您能否编辑您的问题以包含一个包含int x = x; 行之一的函数,这让您感到困惑?
  • /Zw 标志是什么?我想那是 MSVC?
  • 为什么投反对票?这是一个有效的问题。
  • 另请阅读:Point of Declaration

标签: c syntax coding-style


【解决方案1】:

如果你真的有int x = x;,那么它就没什么用处了。这篇文章尝试用自己初始化x,也就是用一个未初始化变量的值。

这可能会抑制一些与未初始化或未使用的变量相关的编译器警告/错误。但是一些编译器也可以捕捉到这些可疑的情况。

从 C 标准的角度来看,这也可能导致未定义的行为。

编辑Random Number Bug in Debian Linux 是一篇关于使用和滥用未初始化变量以及一天可能付出的代价的文章(带有更多链接)。

【讨论】:

  • 如果它是全局的,那么它可能不是未定义的
  • @Koushik 不能用自身初始化全局变量,只能用常量表达式。
  • @Koushik 这将是可编译的代码,但我不太确定它是否具有已定义的行为。
  • @AlexeyFrunze int x = x; 在 C++ 中得到了很好的定义(甚至像其他人所说的那样在示例中使用它)。但是,正如您所说,它可能会导致 C 中未定义的行为。标准引号:“对象的初始值是不确定的。” 以及来自 definitions 的“不确定值”是“未指定的值或陷阱表示”。使用陷阱表示是未定义的行为。
  • @hoody 你会更好int x = 0 去看电影:-)
【解决方案2】:

它可以防止编译器发出变量未使用的警告。

【讨论】:

  • 我会说“一些编译器”。其他编译器比这更聪明。
猜你喜欢
  • 2013-03-30
  • 1970-01-01
  • 2012-04-01
  • 2020-10-02
  • 1970-01-01
  • 2010-09-11
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多