954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

strange windows error

I compiled this code in dev c++ and i got 0 warnings and 0 errors, so it was a clean compile and whenever i run the exe i get this error which says

assembler.exe has encountered a problem and needs to close. And gives me the error report thingy for microsoft. I was wondering if someone else can compile the code and run it on there machine to see if its just my machine or something and if they have advice on why the program is crashing everytime. Thanks in advance

#include <iostream>
#include <fstream>
using namespace std;


int main()
{

string op;
int val1;
int val2;
string val3;
string val4;
int ElementAmount;


ifstream sourcefile;
sourcefile.open("program.code");

while (sourcefile >> op >> val3 >> val4)
{
ElementAmount = ElementAmount + 3;
}

sourcefile.close();

char binary[ElementAmount];

ifstream sourcefile1;
sourcefile1.open("program.code");

while (sourcefile1 >> op >> val1 >> val2)
{

if (op == "storeByte")
{
// storeByte 0 20
}

if (op == "storeWord")
{
// storeWord 0 20
}

if (op == "storeDword")
{
// storeDword 0 20
}

if (op == "moveMR")
{
// moveMR Eax 23
}

if (op == "moveRM")
{
// moveRM 2 Eax
}

if (op == "moveRR")
{
// moveRR Eax Ebx
}

if (op == "add")
{
// add Eax Ebx
}

if (op == "sub")
{
// sub Eax Ebx
}

}

sourcefile1.close();

ofstream binaryfile;
binaryfile.open ("program.bin", ios::out | ios::binary);
binaryfile.write (binary, 512);
binaryfile.close();

}

cam875
Junior Poster
170 posts since Jun 2008
Reputation Points: 12
Solved Threads: 4
 

It's impossible to compile this code: char binary[ElementAmount]; declaraion where ElementAmount is not initialized variable is wrong.
Post another "0 warning 0 errors" snippet ;)...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

You should initialize ElementAmount to zero. Note that if you compile with the -pedantic option, you'll get an error about variable-size array usage, i.e. you might want to change ...

char binary[ElementAmount];

to

char * binary = new char[ElementAmount];
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

and don't forget to delete the memory once you're done with it!

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
#include <iostream>
#include <fstream>
using namespace std;

int main() {

	string op;
	int val1;
	int val2;
	string val3;
	string val4;
	int ElementAmount = 0;

	ifstream sourcefile;
	sourcefile.open("program.code");
	if (!sourcefile.is_open()) {
		cout << "can't open program.code" << endl;
		return 1;
	}
	while (sourcefile >> op >> val3 >> val4) {
		ElementAmount += 3;
	}

	sourcefile.close();

	char* binary = new char[ElementAmount];
	memset(binary, 0, sizeof(binary));
	ifstream sourcefile1;
	sourcefile1.open("program.code");
	if (!sourcefile1.is_open()) {
		cout << "can't open program.code" << endl;
		return 1;
	}

	while (sourcefile1 >> op >> val1 >> val2) {

		if (op == "storeByte") {
			// storeByte 0 20
		}

		if (op == "storeWord") {
			// storeWord 0 20
		}

		if (op == "storeDword") {
			// storeDword 0 20
		}

		if (op == "moveMR") {
			// moveMR Eax 23
		}

		if (op == "moveRM") {
			// moveRM 2 Eax
		}

		if (op == "moveRR") {
			// moveRR Eax Ebx
		}

		if (op == "add") {
			// add Eax Ebx
		}

		if (op == "sub") {
			// sub Eax Ebx
		}

	}

	sourcefile1.close();

	ofstream binaryfile;
	binaryfile.open("program.bin", ios::out | ios::binary);
	binaryfile.write(binary, sizeof(binary));
	binaryfile.close();
	delete[] binary;
}
ivailosp
Junior Poster
129 posts since Apr 2008
Reputation Points: 21
Solved Threads: 22
 

ok thanks ill try that, but what does these two lines of code do?
does it generate a new char element in the binary array for the value of elementamount?
and i dont understand the memset thing

char* binary = new char[ElementAmount];
memset(binary, 0, sizeof(binary));

cam875
Junior Poster
170 posts since Jun 2008
Reputation Points: 12
Solved Threads: 4
 

i dont understand the memset thing

char* binary = new char[ElementAmount]; memset(binary, 0, sizeof(binary));

That code is a beginner's error, and tends to yield bugs that are hard to track down. The memset() call should, presumably, be "memset(binary, 0, ElementAmount);" to set all bytes in the dynamically allocated array binary to zero.

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

my input from the parse comes in from a string but my I need to put that data after im done with it inside a char array and it says it cant convert them but how can i parse something with a char array word by word i dont want an entire line.

cam875
Junior Poster
170 posts since Jun 2008
Reputation Points: 12
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You