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

Click For 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:
Beams of electrons and protons move parallel to each other in the same direction. They ______. a. attract each other. b. repel each other. c. neither attract nor repel. d. the force of attraction or repulsion depends upon the speed of the beams. This is a previous-year-question of CBSE Board 2023. The answer key marks (b) as the right option. I want to know why we are ignoring Coulomb's force?
Thread 'Struggling to make relation between elastic force and height'
Hello guys this is what I tried so far. I used the UTS to calculate the force it needs when the rope tears. My idea was to make a relationship/ function that would give me the force depending on height. Yeah i couldnt find a way to solve it. I also thought about how I could use hooks law (how it was given to me in my script) with the thought of instead of having two part of a rope id have one singular rope from the middle to the top where I could find the difference in height. But the...
I treat this question as two cases of Doppler effect. (1) When the sound wave travels from bat to moth Speed of sound = 222 x 1.5 = 333 m/s Frequency received by moth: $$f_1=\frac{333+v}{333}\times 222$$ (2) When the sound wave is reflected from moth back to bat Frequency received by bat (moth as source and bat as observer): $$f_2=\frac{333}{333-v}\times f_1$$ $$230.3=\frac{333}{333-v}\times \frac{333+v}{333}\times 222$$ Solving this equation, I get ##v=6.1## m/s but the answer key is...

Similar threads

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