C++ Runtime Error Fix: Help Find What I've Done Wrong

AI Thread Summary
The discussion revolves around a C++ runtime error encountered when reading data from a file using MS Visual C++ .NET 2003. The user experiences memory exception errors when attempting to extract information from the file, which contains process data. Key suggestions include avoiding the use of the extraction operator for reading `char*` types due to the risk of buffer overflow and implementing error checking between reads. Instead, using `getline` to read entire lines and then parsing the data into individual tokens is recommended. The user ultimately identifies a memory allocation error in their code as the source of the problem.
enigma
Staff Emeritus
Science Advisor
Gold Member
Messages
1,738
Reaction score
20
Maybe someone can help find what I've done wrong.

I'm using Windows XP, 2.3GHz, 80GB, 256MB, Student Version of MS Visual C++ .NET, 2003 version

I've opened a file up for input, which works fine, but as soon as I try to pull information from the file, it gives exception errors in memory addresses.

If someone can see a blazing error, or knows the fix for this problem, I'd be appreciative.

Relevant code:
Code:
main()
{
	// ---------------------------------------------
	// Opens file: priorities.dat for 
	//

	ifstream inProcesses("priorities.dat", ios::in);

	if (!inProcesses)
	{
		cerr<<"File could not be opened"<<endl;
		exit(1);
	}

	// Ignore first line from file stream
	clearLine( inProcesses );

	// --------------------------------------------
	// Set up Process pointer array

	Process *dataPtr[30]; // Read up to 30 processes

	int i=-1;

	do
	{
		dataPtr[++i] = new Process();

	}while(inProcesses>>*dataPtr[i]);

.
.
.
}

void clearLine ( ifstream &input ) // Ignores a line from file stream
{
	char dump[20];

	input>>dump>>dump>>dump>>dump>>dump>>dump>>dump>>dump>>dump; 
        // This is the line where it quits.
}

// Overloaded input operator from 'Process' object code
istream &operator>>( istream &input, Process &a )
{
	input>>a.UserID>>a.ProcessID>>a.PPID>>a.CLS
		>>a.Priority>>a.STime>>a.TTY>>a.Time>>a.ProcessName; 
        // This is the line which it dumps me if I comment out the first one.

	return input;
}

This is the file it's trying to read:
Code:
     UID   PID  PPID CLS PRI    STIME TTY   TIME CMD
   johny 27708 27707  TS  48 02:58:05 pts/4 0:00 -tcsh
   mikey 27795 27708  TS  48 02:58:52 pts/4 0:00 pine
  smitty 27846 27846  TS  40 02:59:12 pts/4 0:00 blah1
  ralphy 27915 27916  TS  55 02:59:13 pts/4 0:00 blah2
   carly 27925 27926  TS  12 02:59:13 pts/4 0:00 blah3
 
Physics news on Phys.org
Using the extraction operator to read a "char*" is, in general, a bad idea. It just screams, "buffer overflow!" I also notice you don't do any error checking on your stream between reads, another bad idea.

That being said, assuming you've opened the right file, I can't see why it would die on you...

Anyways, the thing you need to investigate is the stream functions "get" and "getline". Generally, you want to use one of these functions to try and grab the contents of an entire line, then use some other means to read the individual tokens, such as string manipulation.


The alternative is to use "string". I don't know how well this works since I tend to avoid using both string and the stream extraction operators (but then again, I've been working on applications where I'm worried about efficiency, so I handle these details myself).


But even if this works, I bet we'll all still be curious what was causing your program to fail. :smile: What I would do in a situation like this is to do the reads one at a time and print them to "cout" to make sure you're getting what you think you're getting.
 
Originally posted by Hurkyl
Using the extraction operator to read a "char*" is, in general, a bad idea. It just screams, "buffer overflow!" I also notice you don't do any error checking on your stream between reads, another bad idea.

That being said, assuming you've opened the right file, I can't see why it would die on you...

Anyways, the thing you need to investigate is the stream functions "get" and "getline". Generally, you want to use one of these functions to try and grab the contents of an entire line, then use some other means to read the individual tokens, such as string manipulation.


The alternative is to use "string". I don't know how well this works since I tend to avoid using both string and the stream extraction operators (but then again, I've been working on applications where I'm worried about efficiency, so I handle these details myself).


But even if this works, I bet we'll all still be curious what was causing your program to fail. :smile: What I would do in a situation like this is to do the reads one at a time and print them to "cout" to make sure you're getting what you think you're getting.

What Hurkyl said, he beat me to this.
 
Code:
#include <iostream>
#include <string>
#include <fstream>

using namespace std ;

struct process
{
	string UID ;
	string PID ;
	string PPID ;
	string CLS ;
	string PRI ;
	string STIME ;
	string TTY ;
	string TIME ;
	string CMD ;
} ;

istream &operator >> (istream &input , process &p )
{
	input >> p.UID >> p.PID >> p.PPID >> p.CLS >> p.PRI
	   >> p.STIME >> p.TTY >> p.TIME >> p.CMD ;

	return input ;
}

void remove_line( istream &input )
{
	string line ;
	getline( input , line ) ;
}

int main( void )
{
	ifstream infile("priorities.dat" , ios::in ) ;

	if( infile.fail() )
	{
		cerr << "File could not be opened" << endl ;
		exit(1) ;
	}

	remove_line( infile ) ;

	process *data[30] ;
	int pcounter = 0 ;

	while( true )
	{
		if( infile.eof() ) break ;

		data[pcounter] = new process ;
		infile >> *data[pcounter] ;
		
		pcounter += 1 ;
	}

	return 0 ;
}

Although I am not using your process class, this code is in good working order.
 
Meh

It was my own stupid error. I wasn't allocating memory correctly, which was brought about by me trying to do:

char a[] = char b[x],

instead of:

strcpy(a,b)

and changing my variable definitions to pointers to a single memory address to pass the compiler errors for

char a = char b lines.

... been a long time since I've done significantly large programs in C++. Matlab has been spoiling me...:frown:
 
Last edited:
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'Collision of a bullet on a rod-string system: query'
In this question, I have a question. I am NOT trying to solve it, but it is just a conceptual question. Consider the point on the rod, which connects the string and the rod. My question: just before and after the collision, is ANGULAR momentum CONSERVED about this point? Lets call the point which connects the string and rod as P. Why am I asking this? : it is clear from the scenario that the point of concern, which connects the string and the rod, moves in a circular path due to the string...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...

Similar threads

Replies
2
Views
3K
Replies
1
Views
2K
Replies
7
Views
3K
Replies
7
Views
4K
Back
Top