Why am I getting warnings and why is the program stopping with an error?

Hi,

The following code compiles but with the following warnings:

read.cpp(186) : warning C4018: '<' : signed/unsigned mismatch
read.cpp(188) : warning C4018: '<' : signed/unsigned mismatch

When debugging, the code runs but then stops with the following error:

'Assignment_1.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcp90d.dll', Symbols loaded.
'Assignment_1.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcr90d.dll', Symbols loaded.
First-chance exception at 0x7715f328 in Assignment_1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0044f66c..
Unhandled exception at 0x7715f328 in Assignment_1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0044f66c..
The program '[5028] Assignment_1.exe: Native' has exited with code 0 (0x0).
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
#define idx(PIC, i, j) \
     *(PIC->img + (i)*PIC->Width + (j))
 
/*** New Data Types ***/
typedef unsigned char BYTE;
 
 
// create data structure to hold image
struct PIC
{
    unsigned int nChannel;
    bool InterLeaved;
    unsigned int Width, Height, Maxval;
    BYTE *img;
};
 
 
 
//function that loads in the header
bool LoadP5Header(ifstream &infile, PIC &pic)
{
    bool rtv = true;
    char buf[16];
    int bufIndex;
    int width, height, maxval;
 
    infile.read(buf, 2); // get the magic number
    buf[2]='\0';
 
    if(buf[0] == 'P' && buf[1] == '5')
	{
        infile.read(buf, 1);
        while(isspace(buf[0])) // Skip white space(s)
		{
            infile.read(buf,1);
		}
 
        // get width
        bufIndex = 0;
        while(bufIndex < 15 && !isspace(buf[bufIndex]))
		{
            bufIndex++;
            infile.read(buf+bufIndex, 1);
        }
        buf[bufIndex] = NULL;  // null terminated string
        width = atoi(buf);
 
        // get height
        infile.read(buf,1);
        while(isspace(buf[0])) // Skip white space(s)
		{
            infile.read(buf,1);
        }
        bufIndex = 0;
        while(bufIndex < 15 && !isspace(buf[bufIndex]))
		{
            bufIndex++;
            infile.read(buf+bufIndex, 1);
        }
        buf[bufIndex] = NULL;  // null terminated string
        height = atoi(buf);
 
       // get Maxval
		infile.read(buf,1);
        while(isspace(buf[0])) // Skip white space(s)
		{
            infile.read(buf,1);
        }
        bufIndex = 0;
        while(bufIndex < 15 && !isspace(buf[bufIndex]))
		{
            bufIndex++;
            infile.read(buf+bufIndex, 1);
        }
        buf[bufIndex] = NULL;  // null terminated string
        maxval = atoi(buf);
 
		// Skip white space(s)
		infile.read(buf,1);
 
        // set the image information in the struct
        pic.InterLeaved = false;
        pic.Width = width;
        pic.Height = height;
		pic.Maxval = maxval;
 
    }
    else rtv = false;
 
    return rtv;
}; // end of LoadP5Header()
 
//function that accepts an infile object and a PIC Object
//and reads in the PIC object
void LoadImage(ifstream &infile, PIC &pic)
{
    infile.read(reinterpret_cast<char *>(pic.img), pic.Width*pic.Height);
}
 
//function that accepts an outstream file and a PIC object to
//and writes the output stream to the PIC object
void WritePGM(ofstream & outfile, PIC pic)
{
    outfile << "P5" << endl;
    outfile << pic.Width << " " << pic.Height << endl;
	outfile << pic.Maxval << endl;
 
    outfile.write(reinterpret_cast<char *>(pic.img), pic.Width*pic.Height);
}
 
 
void zoom_delete(struct PIC *In,struct PIC *Out, int horiz, int vert, int scale)
{
 
 
 
 
 
}
 
 
 
int main()
{  
	int algorithm;
	int scale_factor;
	cout<<"What zooming algorithm do you wish to use? Type '1' for delete scheme, '2' for mean value substitution scheme, '3' for bilinear interpolation"<<endl;
	cin>> algorithm;
	cout<<"What is the factor you wish to zoom by?"<<endl;
	cin>> scale_factor;
	
	ifstream infile_Pin;                /* input file */
    ofstream outfile_Pout;            /* output file */
    char inFileName_Pin[128]="harbor1.pgm";            /* input file name */
    char outFileName_Pout[128]="harbor2.pgm";        /* ouput file name */
	
	int Npixels;
    PIC Pin;            //source image
    PIC Pout;            //output file
 
    /* open input/output file */
    infile_Pin.open(inFileName_Pin, ios::binary);
    outfile_Pout.open(outFileName_Pout, ios::binary);
 
	/* check input file */
    if(!infile_Pin)
    {
        cout<<"Cannot open input file "<< inFileName_Pin <<endl;
        return 1;
    }
 
    if (LoadP5Header(infile_Pin, Pin)) // load pgm (Pin) image header information
    {
        // allocate the memory for the input image
        //the dimensions of the original
        Pin.img = new BYTE[Pin.Width*Pin.Height];
 
        LoadImage(infile_Pin, Pin);
	
		// allocate memory for output images
        Pout.nChannel = 1;
        Pout.InterLeaved = false;
        Pout.Width = Pin.Width / scale_factor;
        Pout.Height = Pin.Height / scale_factor;
		Pout.Maxval = Pin.Maxval;
 
		Npixels = Pout.Width*Pout.Height;
        Pout.img = new BYTE [Npixels];
       
		if(algorithm == 1)
		{
			int pix;
			int i;
			int col;
			int row;
			i = 0;
			row = 0;
			pix = 0;
 
			for(row=0; row < Pin.Height; (row = row + scale_factor))
			{
				for(col=0; col < Pin.Width; (col = col + scale_factor))
				{
					
					Pout.img[i] = Pin.img[(row*Pin.Width + col)];
					i++;
 
				}
 
				cout<<"hello";
 
			}		
 
		}
		else if(algorithm == 2)
		{
		}
		else if(algorithm == 3)
		{
		}
		else
		{
			cout << "Invalid Algorithm Chosen" << endl;
		}  
 
        WritePGM(outfile_Pout, Pout);
	}
}

Open in new window

datopdogg7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alb66Commented:
PIC.Width and PIC.Height are defined "unsigned int" while row and col are defined as "int", so you get the compile warning.
You should define row and col as "unsigned int" too.

At whitch line do you get the error while debugging?
0
peprCommented:
I guess it could be related really to the lines shown in the snippet. The problem is that signed and unsigned integers use the same amount of memory to be stored; however, the content of the memory is interpreted differently. The unsigned value starts from zero to the 2**n - 1 where n i s number of bits. The signed value is able to express rouhly half of the ingerval for negative numbers and half for the positive numbers.

When you write   v_signed < v_unsigned then it is not clear if the signed should be interpreted as "unsigned" or the unsigned should be interpreted as "signed" to do the comparison correctly.

You often want to use the predefined type "size_t" for storing sizes, indexes, etc.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alb66Commented:
Npixels must be declared "unsigned int" too.
0
datopdogg7Author Commented:
Thanks, that fixed the warnings, but how about the error?
0
peprCommented:
Looks like accessing memory via uninitialized pointer. I did not look heavily for details; anyway, do you use the idx macro?. Its definition looks suspicious. You should debug in finer steps to locate the place where the problem appears.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.