这段代码意味着什么

时间:2015-08-13 11:39:31

标签: c linux-kernel

我找到了一个代码段。我不明白。似乎变量__rem根本没用。下面的一行还没有做任何有用的工作:

You answer is good. It helped me alot.I have changed one thing for performance .Moved the bitmap creation to outside of ondraw

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.graphics.Rect;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View {

    private Movie mMovie;
    InputStream mStream;
    long mMoviestart;
    private Context context;

    public GifView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.context = context;
        init();
    }

    public GifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public GifView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    Bitmap movieBitmap  = null;

    private void init() {

        InputStream object = this.getResources().openRawResource(
                R.raw.postloadinganimation);
        mMovie = Movie.decodeStream(object);// context.getResources().getAssets().open("PostLoadingAnimation.gif"));

        movieBitmap = Bitmap.createBitmap(mMovie.width(),
                mMovie.height(), Bitmap.Config.ARGB_8888);      
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);

        super.onDraw(canvas);
        final long now = SystemClock.uptimeMillis();

        if (mMoviestart == 0) {
            mMoviestart = now;
        }

        final int relTime = (int) ((now - mMoviestart) % mMovie.duration());
        mMovie.setTime(relTime);
        // mMovie.draw(canvas, 0, 0);

        Canvas movieCanvas = new Canvas(movieBitmap);
        mMovie.draw(movieCanvas, 0, 0);

        Rect src = new Rect(0, 0, mMovie.width(), mMovie.height());
        Rect dst = new Rect(0, 0, this.getWidth(), this.getHeight());
        canvas.drawBitmap(movieBitmap, src, dst, null);

        this.invalidate();
    }


}

整个代码段如下:

(void)(((typeof((n)) *)0) == ((uint64_t *)0));   \

为什么这里没用无用的代码?

1 个答案:

答案 0 :(得分:12)

1。 (void)(((typeof((n)) *)0) == ((uint64_t *)0));

请参阅Linux/include/asm-generic/div64.h

  

那里有不必要的指针比较   检查类型安全性(n必须为64位)

示例:

n必须为int,但为short

void main()
{
    short n;
    (void)(((typeof((n)) *)0) == ((int *)0));
}

我们收到警告: 的 comparison of distinct pointer types lacks cast

编译:{{1​​}}

编译器版本:gcc -o main main.c

<强>结论:

指针比较并非无用。如果传递给gcc (GCC) 4.9.2 20141101 (Red Hat 4.9.2-1)的变量类型错误,它会生成警告。

2。 __rem

大括号包围的代码是gcc语句表达式。 do_div()可以说是__rem的返回值。

示例:

do_div()

输出:#include <stdio.h> #define do_div(n,base) ({ \ int __rem = n % base; \ n /= base; \ __rem; \ }) int main() { int a = 9; int b = 2; int c = 0; printf("%i / %i = ", a, b); c = do_div(a, b); printf("%i, reminder = %i\n", a, c); return 0; }

在上面的示例中,9 / 2 = 4, reminder = 1相当于c = do_div(a, b)

<强>结论:

c = ({int rem = a % b; a /= b; rem;})并非没用,它是&#34;返回值&#34; __rem