调试器跳过整个 if 语句
Posted
技术标签:
【中文标题】调试器跳过整个 if 语句【英文标题】:Debugger skipping a whole if statement 【发布时间】:2017-07-19 18:01:51 【问题描述】:/**
* Copies a BMP piece by piece, just because.
* All we have to do is change all the red pixels to
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
// ensure proper usage
if (argc != 3)
fprintf(stderr, "Usage: ./copy infile outfile\n");
return 1;
// remember filenames
char *infile = argv[1];
char *outfile = argv[2];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
fprintf(stderr, "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;
// 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;
// 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);
RGBTRIPLE red = 0, 0, 255;
int same = 0;
RGBTRIPLE white = 255, 255, 255;
if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
same = 1;
if(same == 0) //copy the pixel, if the pixel isn't red
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
if(same == 1)
fwrite(&white, 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);
// success
return 0;
我的问题更具体地在于这段代码:
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
RGBTRIPLE red = 0, 0, 255;
int same = 0;
RGBTRIPLE white = 255, 255, 255;
if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
same = 1;
if(same == 0) //copy the pixel, if the pixel isn't red
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
if(same == 1)
fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
我正在尝试将 bmp 文件复制到另一个文件,但去掉任何红色并将其替换为白色(大部分代码不是我的工作,我应该为作业编辑它的预写代码) , 复制功能有效,但由于某种原因检测红色并将其替换为白色不起作用。奇怪的是,当我使用调试器时,它完全跳过了这部分代码:
if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
same = 1;
这是我检测像素是否为红色的地方,调试器就像该行不存在一样。
【问题讨论】:
&stuff.other
将返回一个地址,而不是实际值。那是你要的吗?如果我是编译器,我会优化该行,因为显然表达式总是错误的。
您在比较地址而不是内容。摆脱 &。
【参考方案1】:
不要比较地址,您根本不需要使用int same
变量。
if(red.rgbtRed == triple.rgbtRed && red.rgbtGreen != triple.rgbtRed && red.rgbtBlue != triple.rgbtRed)
fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
else
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
【讨论】:
【参考方案2】:不要比较地址,它总是会失败。比较值,因为这会导致简单的整数比较
if(red.rgbtRed == triple.rgbtRed && red.rgbtGreen != triple.rgbtRed && red.rgbtBlue != triple.rgbtRed)
same = 1;
【讨论】:
就是这样!地址和指针仍然是新手,所以我还没有完全掌握它。谢谢!【参考方案3】:你的编译器可能会检测到这个“if”条件总是错误的,它会优化这些行。因此,这部分代码不会出现在您的可执行文件中,您的调试器也看不到它。
【讨论】:
以上是关于调试器跳过整个 if 语句的主要内容,如果未能解决你的问题,请参考以下文章