[comp.lang.c++] Turbo-C++ problem when compiling without debugging info

wallis@labc.enet.dec.com (Barry L. Wallis) (08/13/90)

The attached program exhibits some strange behavior. When compiled and linked
with debugging information turned on (both in the IDE or on the command line)
the prgram works fine. However, when the debugging information is turned off
(-v- on the command line). Needless to say this makes debugging very difficult.

The classes #included using qutoation marks are from the Turbo-C++ class
library. The others are the normal Turbo-C++ header files.

Any ideas?


// SUBST.CPP	Filter by substituting words
//
#include <ctype.h>
#include <fstream.h>
#include <iostream.h>
#include <iomanip.h> // Must appear after <iostream.h>
#include "assoc.h"
#include "dict.h"
#include "strng.h"

const int word_size = 80; // Maximum size of an input word

// Prototype
void load_dictionary(Dictionary& dict, char* filespec);

int main(int argc, char* argv[]) {
	if (argc <= 1) {
		cerr << "usage: subst wordlist_filespec <input >output\n";
		exit(EXIT_FAILURE);
	}
	Dictionary& dict = *(new Dictionary);
	load_dictionary(dict, argv[1]);
	cin.unsetf(ios::skipws);
	char in_word[word_size];
	do {
		while (cin.good() && isspace(cin.peek())) 
			cout << (char)cin.get();
		if (!cin.good()) break;
		if (isalpha(cin.peek())) {
			for (int i = 0; 
				 cin.good() && 
					i < word_size - 1 && 
					isalnum(cin.peek()); 
				 i++) in_word[i] = cin.get();
				 in_word[i] = '\0';
			String out_word = in_word;
			Association& assoc = dict.lookup(out_word);
			if (assoc != NOOBJECT) {
				cout << assoc.value();
			} else {
				cout << out_word;
			}
		} else {
			cin >> setw(word_size) >> in_word;
			cout << in_word;
		}
	} while (cin.good());
}

void load_dictionary(Dictionary& dict, char* filespec) {
	ifstream input(filespec);
	char word[word_size];
	while (input.good()) {
		input >> setw(word_size) >> word;
		String& word1 = *(new String(word));
		input >> setw(word_size) >> word;
		String& word2 = *(new String(word));
		Association& assoc = *(new Association(word1, word2));
		dict.add(assoc);
	}
}

I read comp.lang.c++ regularly, but, e-mail shouldn't bounce either. 

Thanks for any help you can give (especially a dumb mistake).
---
Barry L. Wallis			USENET: wallis@labc.dec.com
Database Consultant		Prodigy (don't laugh): DNMX41A
U.S. DECtp Resource Center	DECUServe: EISNER::WALLIS (not on the net yet)
Los Angeles, CA			"No one voted for me, I represent myself"
---

cline@cheetah.ece.clarkson.edu (Marshall Cline) (08/13/90)

In article <14509@shlump.nac.dec.com> wallis@labc.enet.dec.com (Barry L. Wallis) writes:
>
>The attached program exhibits some strange behavior. When compiled and linked
>with debugging information turned on (both in the IDE or on the command line)
>the prgram works fine. However, when the debugging information is turned off
>(-v- on the command line). Needless to say this makes debugging very difficult
>
>The classes #included using qutoation marks are from the Turbo-C++ class
>library. The others are the normal Turbo-C++ header files.
>
>Any ideas?

In my TC++ review in the most recent `C++ Report' (vol.2, #7, Jul/Aug 90),
I reported finding a very unusual compiler bug which behaved like this.

If you call a virtual member function from an *inline* function in a base
class, the virtual function call gets bound *statically*.  Ex:

	class Stack {
	public:
	  virtual int length() const = 0;	// Return Stack's curr `length'
	          int empty()  const { return length() == 0; }	// Is it empty?
	  //...
	};

Under the following rather special conditions, TC++ generates bad code:
 IF  * you compile it with command-line `tcc' rather than integrated `tc'.
 AND * you compile it with debugging turned off (-v-)
 AND * you compile it with inlines turned on (-vi)

The work-arounds for this are obvious.  Any one of the following will do:
    * out-line the `Stack::empty()' function
 OR * use `tc' (strange, but `tc' compiles it correctly)
 OR * use `tcc' but use -v (-v turns off inlining unless followed by -vi)
 OR * use `tcc' but use -vi-

Borland has been made aware of the bug.

Marshall Cline

PS: I have created a bug-list repository for TC++ which has work-arounds.
It's available by anonymous ftp from sun.soe.clarkson.edu [128.153.12.3]
in the file ~ftp/pub/Turbo-C++/bug-report

If you don't have ftp access, write me at my USmail address below, and
I'll USmail it to you for a $5 processing fee (sorry for the fee, but
I don't want to be inundated with requests that will cost me hours of
my time :-).

--
==============================================================================
Marshall Cline / Asst.Prof / ECE Dept / Clarkson Univ / Potsdam, NY 13676
cline@sun.soe.clarkson.edu / Bitnet:BH0W@CLUTX / uunet!clutx.clarkson.edu!bh0w
Voice: 315-268-3868 / Secretary: 315-268-6511 / FAX: 315-268-7600
Career search in progress; ECE faculty; research oriented; will send vita.
PS: If your company is interested in on-site C++/OOD training, drop me a line!
==============================================================================