PDA

View Full Version : To those who code in C++...


c15zyx
07-24-2002, 04:05 AM
Does anyone have an explanation for this...?

compile this simple code under both Devtools g++/c++ 3.1 and c++ 2.95.

#include <iostream>

using std::cout;
using std::cin;

int main()
{
char a[8];

cin.getline(a, 8);
cout << a;

return 0;
}


Version 3.1 requires a second return! Anyone know if this is due to the different C++ libs used in v3 or if this is the way it's supposed to work (hope not)? Hope this is fixed.

Seb
07-24-2002, 04:53 AM
I get no different behaviour whatsoever using c++3 (gcc 3.1) or c++ (gcc 2.95.2) to compile this -- using the April (beta) version of the devtools. Need to hit return once after my input to get it echoed and the program quit.

c15zyx
07-24-2002, 05:21 AM
Oh, well I'm using 3.1 from cvs (1200 20020420). Didn't specify any unusual options during the install process though. I guess everything will be ok when jaguar comes out. Thanks.

sao
07-27-2002, 01:42 PM
Another issue with the gcc3 compiler is an incompatibility for C++ ABIs between gcc2 and gcc3. In practice, this means that C++ programs compiled with gcc3 cannot link to libraries compiled with gcc2.


Cheers...

sao
07-28-2002, 02:04 PM
http://developer.apple.com/techpubs/macosx/ReleaseNotes/GCC3.html

says:

• Recompile all your C++ code, including libraries and frameworks. GCC 3 has a new application binary interface (ABI) for C++, including changes to name mangling, exception handling, and class layout and alignment. You do not need to recompile C or Objective-C code.


The version of gcc3 that's tagged gcc-1151 will be released with Jaguar.


Cheers...

c15zyx
09-16-2002, 10:43 AM
Recently got jaguar and I noticed that I am still having this problem. I fresh installed the os and software (wiped drive) w/ the retail dev tools + the august update (1161), so it's not the 'old baggage' that's causing the problem. Using GCC2 works as expected, but GCC3 is screwing it up.

Try this... both getlines requires 2 inputs and the second getline gets the 2nd input of the first getline. even with cin.sync() or cin.clear(), the second getline seems to keep getting input from the buffer.


#include <iostream>

using namespace std;

int main()
{
char a[8];

cout << "A1: ";
cin.getline(a, 8);
cout << "a is " << a << endl;

cout << "A2: ";
cin.getline(a, 8);
cout << "a is " << a << endl;
return 0;
}


A sample dump is:

A1: TEST1
TEST2
a is TEST1
A2: TEST3
a is TEST2


what is going on here?

[EDIT]
Just tried it out on sun solaris machines running gcc 3.0.3, and it works fine there. Wondering why i am getting this problem, as I am using the retail tools.

hayne
09-19-2002, 04:37 PM
This is a known bug in gcc 3.1 (it got fixed in gcc 3.1.1).

For more details, go to the gcc bug tracking page:
http://gcc.gnu.org/cgi-bin/gnatsweb.pl
and View bug # 6648

Unfortunately, the latest Developer Tools use gcc 3.1,
so it seems we are stuck with this bug until the next update.

But here's a replacement function that I wrote:

// myGetline:
// The gcc 3.1 version of the 'getline' function has a bug - it requires
// two delimiters (e.g. newlines) before it accepts the input
// The 'myGetline' function provides a replacement for 'getline'
// Sample use: char name[100]; myGetline(cin, name, 100);

istream& myGetline(istream& in, char* buffer, streamsize n, char delim = '\n')
{
if (in.peek() == delim)
{
// the gcc 3.1 version of the 'get' function seems to have a bug
// that causes it to fail if there are no characters before the
// delimiter (e.g. an empty line)

buffer[0] = '\0';
}
else
{
in.get(buffer, n, delim);
}

if (in.good() && strlen(buffer) < n - 1)
{
// eat the delimiter
in.ignore(1);
}
return in;
}