1

我有一个行为不同的变量,具体取决于我在同一函数中声明的位置。我想了解正在发生的事情。

在这个函数中,我在两个地方都包含了变量声明和初始化,这两个地方产生了不同的结果,只是为了说明正在发生的事情。当我运行程序时,我没有在任何一个地方两次声明变量,只有一次。

我指的是变量 temp,我已经评论了场景 1 和场景 2

C 函数

    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 4) // we need four arguments
        {
            printf("Usage: copy infile outfile\n");
            return 1;
        }

        // remember filenames
        char* infile = argv[2];
        char* outfile = argv[3];

        // the factor
        int n = argv[1][0] - '0';

        // SCENARIO 1) HERE HERE HERE HERE THIS IS THE VARIABLE I AM DISCUSSING!!!!!!!
->      int temp = n; // WHEN DECLARED HERE AND INITIALIZED AT TIME OF CREATION
                      // gdb displays its value equal to the value of n


        // open input file 
        FILE* inptr = fopen(infile, "r");
        if (inptr == NULL)
        {
            printf("Could not open %s.\n", infile);
            return 2;
        }

        // open output file
        FILE* outptr = fopen(outfile, "w");
        if (outptr == NULL)
        {
            fclose(inptr);
            fprintf(stderr, "Could not create %s.\n", outfile);
            return 3;
        }

        // read infile's BITMAPFILEHEADER
        BITMAPFILEHEADER bf;
        fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

        // read infile's BITMAPINFOHEADER
        BITMAPINFOHEADER bi;
        fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

        // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
        if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
            bi.biBitCount != 24 || bi.biCompression != 0)
        {
            fclose(outptr);
            fclose(inptr);
            fprintf(stderr, "Unsupported file format.\n");
            return 4;
        }

        //gdb break to check the previous values


        // SCENARIO 2) HERE HERE HERE HERE THIS IS THE VARIABLE I AM DISCUSSING!!!!!!!!
->      int temp = n; // WHEN DECLARED HERE AND INITIALIZED THE VARIABLE HERE
                      // gdb displays its value equal to 0 and not n

        // write outfile's BITMAPFILEHEADER
        fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

        // write outfile's BITMAPINFOHEADER
        fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

        // determine padding for scanlines
        int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

        //gdb break to check the new values

        // iterate over infile's scanlines
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            // iterate over pixels in scanline
            for (int j = 0; j < bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

            // skip over padding, if any
            fseek(inptr, padding, SEEK_CUR);

            // then add it back (to demonstrate how)
            for (int k = 0; k < padding; k++)
                fputc(0x00, outptr);
        }

        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

        // that's all folks
        return 0;
    }

当代码以任何方式更改 n 或 temp 时,为什么我会得到不同的值?

4

1 回答 1

0

因此,如果我理解您的代码,您永远不会阅读tempn初始化之后。在第二种情况下,编译器可能已经发现了这一点(取决于优化级别)并将其删除。

于 2013-04-12T06:00:52.433 回答