Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 07-24-2002, 03:05 AM   #1
c15zyx
Prospect
 
Join Date: Jan 2002
Posts: 16
To those who code in C++...

Does anyone have an explanation for this...?

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

Code:
#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.
c15zyx is offline   Reply With Quote
Old 07-24-2002, 03:53 AM   #2
Seb
Prospect
 
Join Date: Jul 2002
Location: Durham, England
Posts: 2
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.
Seb is offline   Reply With Quote
Old 07-24-2002, 04:21 AM   #3
c15zyx
Prospect
 
Join Date: Jan 2002
Posts: 16
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.
c15zyx is offline   Reply With Quote
Old 07-27-2002, 12:42 PM   #4
sao
Moderator
 
Join Date: Jan 2002
Location: Singapore
Posts: 4,237
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 is offline   Reply With Quote
Old 07-28-2002, 01:04 PM   #5
sao
Moderator
 
Join Date: Jan 2002
Location: Singapore
Posts: 4,237
http://developer.apple.com/techpubs/...otes/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...
sao is offline   Reply With Quote
Old 09-16-2002, 09:43 AM   #6
c15zyx
Prospect
 
Join Date: Jan 2002
Posts: 16
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.

Code:
#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:
Code:
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.

Last edited by c15zyx; 09-16-2002 at 10:04 AM.
c15zyx is offline   Reply With Quote
Old 09-19-2002, 03:37 PM   #7
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,939
known bug in gcc 3.1

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:

Code:
// 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;
}
hayne is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 11:49 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.