Problems with char arrays in dynamic memory...

I can't seem to pin down why my program, when run, will puke out this:


... omitted ...
a.cpp:29: error: stray '\251' in program
a.cpp:29: error: stray '\255' in program
a.cpp:29: error: stray '\255' in program
a.cpp:29: error: stray '\131' in program
a.cpp:29: error: stray '\209' in program
a.cpp:29: error: stray '\1' in program
a.cpp:29: error: stray '\141' in program
a.cpp:29: error: stray '\20' in program
a.cpp:29: error: stray '\131' in program
a.cpp:29: error: stray '\253' in program
a.cpp:29: error: stray '\252' in program
a.cpp:29: error: stray '\15' in program
a.cpp:29: error: stray '\138' in program
a.cpp:29: error: stray '\2' in program
a.cpp:29: error: stray '\136' in program
a.cpp:29: error: stray '\7' in program
a.cpp:29: error: stray '\247' in program
a.cpp:29: error: stray '\233' in program
... omitted ...
a.cpp:29: error: stray '\236' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29: error: stray '\233' in program
a.cpp:29: error: stray '\186' in program
a.cpp:29: error: stray '\149' in program
a.cpp:29: error: stray '\255' in program
a.cpp:29: error: stray '\255' in program
a.cpp:29:409: warning: null character(s) ignored
a.cpp:29: error: stray '`' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29:561: warning: null character(s) ignored
a.cpp:29: error: stray '\128' in program
a.cpp:29:565: warning: null character(s) ignored
a.cpp:29: error: stray '\128' in program
a.cpp:29:581: warning: null character(s) ignored
a.cpp:29: error: stray '\128' in program
a.cpp:29:585: warning: null character(s) ignored
a.cpp:29: error: stray '\128' in program
a.cpp:29:609: warning: null character(s) ignored
a.cpp:29: error: stray '\134' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29:613: warning: null character(s) ignored
a.cpp:29: error: stray '\150' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29:617: warning: null character(s) ignored
a.cpp:29: error: stray '\166' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29:621: warning: null character(s) ignored
a.cpp:29: error: stray '\180' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29:625: warning: null character(s) ignored
a.cpp:29: error: stray '\194' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29:629: warning: null character(s) ignored
a.cpp:29: error: stray '\208' in program
a.cpp:29: error: stray '\128' in program
a.cpp:29:637: warning: null character(s) ignored
a.cpp:29:655: warning: null character(s) ignored
a.cpp:29:666: warning: null character(s) ignored
a.cpp:29:681: warning: null character(s) ignored
a.cpp:29:697: warning: null character(s) ignored
a.cpp:29:713: warning: null character(s) ignored
a.cpp:29:727: warning: null character(s) ignored
a.cpp:29:740: warning: null character(s) ignored
a.cpp:29:754: warning: null character(s) ignored
a.cpp:29:761: warning: null character(s) ignored
a.cpp:29:1059: warning: no newline at end of file


I'm using g++ to compile my program, and using g++ within my source to compile another program... my main program compiles with no complaints, but while executing it, I get that stuff spilling out from g++... Had no probs until I tried to use dynamically allocated char arrays, instead of static ones.

Here is my source, with comments. The comments reguarding my problem are within /*** ***/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdio>
#include <windows.h>
#include <iostream>
#include <sys/stat.h>
using namespace std;

/*** Declare dynamically allocated arrays?? ***/
char *srcFile, *outFile, *execCompile, *execStrip, *execPack;
int x;

// Usage info
void usage(char** argv) {
	cout << "Usage: " << argv[0] << " [FILE]\n" << "\nOptions:\n"
			<< "-h, --help\tDisplay this message and exit\n" << "\nNotes:\n"
			<< "Do not use an extension in [FILE].\n" << "e.g. '" << argv[0]
			<< " mysource', NOT '" << argv[0] << " mysource.cpp'" << endl;
}
// check if a file exsists
int file_exist(char *filename) {
	struct stat buffer;
	return (stat(filename, &buffer) == 0);
}
// generic error message
int error() {
	cerr << "Error: Unknown" << endl;
	return 1;
}
// generic success message
void done() {
	cout << "Done" << endl;
}

int main(int argc, char** argv) {

	/*** Initialize the dynamically allocated char arrays?? ***/
	srcFile = new char [strlen(argv[1])+6]; // initialize srcFile with the length of argv[1] + ".cpp" + any null terminations
	outFile = new char [strlen(argv[1])+6]; // initialize outFile the same way
	execCompile = new char [strlen(outFile)+strlen(srcFile)+31]; // init execCompile with length of outFile + srcFile + string that it will be put in it + null terms
	execStrip = new char [strlen(outFile)+18]; // init with outFile + string length + nullterms
	execPack = new char [strlen(outFile)+37]; // init with outFile + string length + nullterms

	sprintf(srcFile, "%s.cpp", argv[1]); // print "argv[1].cpp" to srcFile
	sprintf(outFile, "%s.exe", argv[1]); // same thing
	if (argc > 1) {
		if (strstr(argv[1], "-h")) { // find -h in argv[1]
			usage(argv); // print help & usage info
			return 0;
		}
		sprintf(execCompile, "g++ -Wall -Os -o %s %s", outFile, srcFile); // print "g++ -Wall -Os -o outFile.exe inFile.cpp" to execCompile
		sprintf(execStrip, "strip %s", outFile); // printf "strip outFile" to execStrip
		sprintf(execPack, "upx --best --ultra-brute %s", outFile); // print "upx --best --ultra-brtue outFile.exe" to execPack
		if (file_exist(srcFile)) { // check if specified file exsists
			cout << "Compiling ... ";
			x = system(execCompile); // execute command line to compile srcFile
			if (x == 0) done();
			else error();
			cout << "Stripping ... ";
			x = system(execStrip); // execute CL to strip the resulting executable
			if (x == 0) done();
			else error();
			cout << "Packing   ... ";
			x = system(execPack); // execute CL to pack the resulting exe
			if (x == 0) done();
			else error();
		} else {
			cerr << "Error: " << srcFile << ": No such file" << endl; // error if file doesn't exsist
			return 1;
		}

	} else {
		cerr << "Error: No file specified" << endl; // error if no filename was given on CL
		cout << "Type " << argv[0] << " --help for more info." << endl;
		return 1;
	}

	/*** Clear out the memory allocations?? ***/
	delete[] srcFile;
	delete[] outFile;
	delete[] execCompile;
	delete[] execStrip;
	delete[] execPack;
	return 0;
}

/*
 * TODO: Prompt for filename if not specified in CL arg.
 * TODO: Support working with multiple files.
 * TODO: Use dynamically allocated char arrays
 * TODO: Add more options for output control
 * TODO: Allow specifying '.cpp' in filename
 */
By the way, the program is supposed to use GNU g++ to compile a C/C++ source, then strip it using GNU strip, then pack it using UPX.

Like I said, works fine using static arrays, but I guess I'm not implementing the dynamic arrays correctly or something.
Normally you get that when you have some UTF-8 characters in the file. Try looking with a hex editor at the file and finding these stray characters and deleting them.
Apperently, the cpp was really an exe, it somehow got renamed to .cpp... IDFK how that happened...

So turns out my source was fine. Lol.
Last edited on
Topic archived. No new replies allowed.