如何处理不良指针 [英] how to handle bad pointer

查看:78
本文介绍了如何处理不良指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给出图像第一时间的路径(c:\ b.jpeg)并调用ReadImage()可以,但是
当我第二次调用ReadImage()(c:\ banner3.jpeg)时,调试断言失败.
当我第二次读取szFileName时,确实显示了错误的指针.
如何解决?

I give the path of image fist time(c:\b.jpeg) and call ReadImage() it works but
when I call ReadImage() second time (c:\banner3.jpeg) it gives debug assertion failed.
Actully when I read the szFileName on second time call it shows bad pointer.
How to solve it?

BITMAPINFOHEADER* ReadImage(char* szFileName)
{
	size_t nSize = 0;
	size_t nRead = 0;
	size_t nHeaderSize = 14;
	BITMAPINFOHEADER* pBitmap = NULL;
	// try to open the file.
	FILE* fp=fopen(szFileName, "rb");
	if (fp==NULL)
	{
		printf("%s: failed to open with errno %d.\n", szFileName, errno);
		return NULL;
	}

	// get the size if the bitmap
	fseek(fp, 0, SEEK_END);
	nSize = ftell(fp);
	fseek(fp, (long)nHeaderSize, SEEK_SET ); // really dirty skip the bitmap file header.
	nSize -= nHeaderSize; // adjust the size to fit the mem size

	// get the memory for the bitmap.
	pBitmap = (BITMAPINFOHEADER*)malloc(nSize);
	if (pBitmap==NULL)
	{
		printf("%s: failed to alloc memory.\n", szFileName);
		fclose(fp);
		return NULL;
	}

	// read data from disk
	nRead = fread(pBitmap, 1, nSize, fp);
	if (nRead!=nSize)
	{
		free(pBitmap);
		printf("%s: failed to read image.\n", szFileName);
		fclose(fp);
		return NULL;
	}
	
	fclose(fp);
		return pBitmap;
}


void main( int argc, char *argv[])
{
	DrmErrorCode_t nRetCode;
	FC3Template_t hTemplate1, hTemplate2;
	FC3Encode_t hEncoder;
	FC3Match_t hMatcher;
	double dScore;
	char image1[]={'a'};
	char image2[]={'h'};

scanf("%s",&image1);

	// Checking the arguments
	if (argc!=1)
	{
		printf("Usage: %s Image1.bmp, Image2.bmp\n", argv[0]);
		//return 1;
	}

	// Loading the image for quality check
	BITMAPINFOHEADER* pBitmap1 = NULL;
	BITMAPINFOHEADER* pBitmap2 = NULL;
	pBitmap1 = ReadImage(argv[0]);
	//pBitmap1 = ReadImage(argv[1]);
	if (pBitmap1==NULL)
	{
		printf("Error while reading Image1\n");
		//return 2;
	}

	scanf("%s",&image2);
pBitmap2 = ReadImage(argv[1]);

推荐答案

尝试一下:):
Try this :) :
// free((void*)szFileName);


BITMAPINFOHEADER* ReadImage(char* szFileName)
{
	size_t nSize = 0;
	size_t nRead = 0;
	size_t nHeaderSize = 14;
	BITMAPINFOHEADER* pBitmap = NULL;
	// try to open the file.
	FILE* fp=fopen(szFileName, "rb");
	if (fp==NULL)
	{
		printf("%s: failed to open with errno %d.\n", szFileName, errno);
		return NULL;
	}
 
	// get the size if the bitmap
	fseek(fp, 0, SEEK_END);
	nSize = ftell(fp);
	fseek(fp, (long)nHeaderSize, SEEK_SET ); // really dirty skip the bitmap file header.
	nSize -= nHeaderSize; // adjust the size to fit the mem size

	// get the memory for the bitmap.
	pBitmap = (BITMAPINFOHEADER*)malloc(nSize);
	if (pBitmap==NULL)
	{
		printf("%s: failed to alloc memory.\n", szFileName);
		fclose(fp);
		return NULL;
	}
 
	// read data from disk
	nRead = fread(pBitmap, 1, nSize, fp);
	if (nRead!=nSize)
	{
		free(pBitmap);
		printf("%s: failed to read image.\n", szFileName);
		fclose(fp);
		return NULL;
	}
	
	fclose(fp);
        // You can't free this parameter, because you use argv array item here
	//free((void*)szFileName); 
	return pBitmap;
}

void main( int argc, char *argv[])
{
	DrmErrorCode_t nRetCode;
	FC3Template_t hTemplate1, hTemplate2;
	FC3Encode_t hEncoder;
	FC3Match_t hMatcher;
	double dScore;
	char image1[]={'a'};
	char image2[]={'h'};
 
        scanf("%s",&image1);
 
	// Checking the arguments
	if (argc!=3)
	{
		printf("Usage: %s Image1.bmp, Image2.bmp\n", argv[0]);
		//return 1;
	}
 
	// Loading the image for quality check
	BITMAPINFOHEADER* pBitmap1 = NULL;
	BITMAPINFOHEADER* pBitmap2 = NULL;
	pBitmap1 = ReadImage(argv[0]);
	if (pBitmap1==NULL)
	{
		printf("Error while reading Image1\n");
	}
 
	scanf("%s",&image2);
        pBitmap2 = ReadImage(argv[1]);
	if (pBitmap2==NULL)
	{
		printf("Error while reading Image2\n");
	}
        // You need to free memory used by bitmaps
        free(pBitmap1);
        free(pBitmap2);
}



请查看代码中的注释. ReadImage和主要进程的结尾



Please see comments in code. The end of ReadImage and main procs


这篇关于如何处理不良指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆