Fedora Linux Support Community & Resources Center
  #1  
Old 15th July 2007, 10:48 PM
Bachmann Offline
Registered User
 
Join Date: Nov 2005
Age: 26
Posts: 123
Odd compiler disagreement

Hi, I am a student who recently started a summer C++ course. My first homework assignment just came back and I did not do so well :-(

The reason I come here is because while it compiled and ran fine with my g++. It caused an infinite loop in my professors system. (he is using visual c++ 5)

My professor pointed out the issue in the while loop. He says to use *moby instead of moby for the test. Now at first I thought it was a difference between the microsoft compiler and the g++ compiler.... but then I went into windows and downloaded devc++ which uses g++ for its compiling and had the same infinite loop.

Does anyone know why this runs fine with my g++ in linux but not the windows g++ with devc++?

Code:
#include<iostream>
using namespace std;

/**
 * Matt Bachmann
 * CS 331
 * HW1 Due: July 9th 2007
 * Main method shows different ways of traversing strings
 */ 
int main() {
	char mary[25] = "Mary had a little lamb.";
	char* moby = "Thar she blows!";
	
	cout << strlen(mary) << endl << endl;
	for(unsigned int i = 0; i < strlen(mary); i++) {
		for(unsigned int a = i; a < strlen(mary); a++) {
			cout << mary[a];
		}
		cout << endl;
	}
	cout << endl;
	cout << strlen(moby) << endl << endl;
	while(moby != '\0') {
		cout << moby << endl;
		moby++;
	}
}
Reply With Quote
  #2  
Old 15th July 2007, 11:45 PM
clearer's Avatar
clearer Offline
Registered User
 
Join Date: Sep 2004
Location: Odense, Denmark
Age: 31
Posts: 580
moby is a pointer and not a char.
Your while loop tests when the pointer is the same as '\0', which it never will be at some point in time -- but likely not untill you've given up.

You need to dereference the pointer with the * operator. The while loop becomes:
Code:
while (*moby != '\0') {
cout << moby << endl;
++moby;
}
Calling ++moby instead of moby++ can be faster (not always is though, but it's never slower).

Alternatively you could use
Code:
while (*moby != '\0') 
cout << moby++ << endl;
which has the same result.
__________________
Why you shouldn't use RAID3/4/5
The thoughts of a madman
==========================
I am not a lawyer.

Last edited by clearer; 15th July 2007 at 11:50 PM.
Reply With Quote
  #3  
Old 16th July 2007, 12:00 AM
clearer's Avatar
clearer Offline
Registered User
 
Join Date: Sep 2004
Location: Odense, Denmark
Age: 31
Posts: 580
Btw, you may ask your professor to update his compiler if he is teaching standard C++ as MSVC prior to version 7 (they where marketed as Windows compiler as opposed to being C++ compilers) are know not to following standards. This could potentially lead to failing grades because his software is not up to date.

The MinGW compiler (which is used in Dev-Cpp -- it's a port of GCC for Windows) is recommendable.
__________________
Why you shouldn't use RAID3/4/5
The thoughts of a madman
==========================
I am not a lawyer.
Reply With Quote
  #4  
Old 16th July 2007, 12:05 AM
Bachmann Offline
Registered User
 
Join Date: Nov 2005
Age: 26
Posts: 123
Thanks, but whats really confusing me is that it runs fine, the code works when I compile it with my g++.

I understand the code itself is wrong, but why does it seem to run fine? If you or someone else compiles and run it could you tell me if it runs the same way.

As far as my professor using an outdated visual c++ he has mentioned that newer versions are closer to standards and if anyone has issues with different versions to discuss it with him.

So what I am getting at is that it works with g++ in linux but not windows g++ (devc++) or the microsoft compiler.

I accept that the code is wrong and wont make the mistake again, but I am worried that my compiler is steering me wrong and I don't want to loose anymore points because of it.... (for my current assignment I am in windows and using devc++, but I like linux better so I would like to track this down.

thanks again!

Last edited by Bachmann; 16th July 2007 at 12:08 AM.
Reply With Quote
  #5  
Old 16th July 2007, 04:04 AM
Bachmann Offline
Registered User
 
Join Date: Nov 2005
Age: 26
Posts: 123
I wanted to give some more info:

First here is me compiling and running the code

$ g++ HW1.cpp
$ ./a.out
23

Mary had a little lamb.
ary had a little lamb.
ry had a little lamb.
y had a little lamb.
had a little lamb.
had a little lamb.
ad a little lamb.
d a little lamb.
a little lamb.
a little lamb.
little lamb.
little lamb.
ittle lamb.
ttle lamb.
tle lamb.
le lamb.
e lamb.
lamb.
lamb.
amb.
mb.
b.
.

15

Thar she blows!
har she blows!
ar she blows!
r she blows!
she blows!
she blows!
he blows!
e blows!
blows!
blows!
lows!
ows!
ws!
s!
!

and thats where it stops, no infinite loop
and I got the version its g++ 4.1.2, fedora 7 if it matters
Reply With Quote
  #6  
Old 16th July 2007, 01:08 PM
clearer's Avatar
clearer Offline
Registered User
 
Join Date: Sep 2004
Location: Odense, Denmark
Age: 31
Posts: 580
I use the same compiler and compiled the original code just to see what the result was. When I ran your original code, I got garbage output. No infinite loop though -- just garbage (as suspected).

I don't know why you get a different result.
__________________
Why you shouldn't use RAID3/4/5
The thoughts of a madman
==========================
I am not a lawyer.
Reply With Quote
  #7  
Old 21st July 2007, 01:01 PM
pfinet Offline
Registered User
 
Join Date: Jul 2007
Location: Italy
Posts: 20
I compiled the original code with the same compiler and I got garbage too.
Reply With Quote
Reply

Tags
compiler, disagreement, odd

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler Option GCC, Intel Compiler, PathScale mounterriver Using Fedora 2 8th November 2009 02:31 PM
C compiler!!! panoramix222000 Programming & Packaging 18 9th September 2008 06:19 PM
C++ compiler Stefan21 Using Fedora 13 7th December 2007 07:45 PM
FC4: which compiler and where is it? longman Using Fedora 3 23rd May 2006 12:47 AM
GNU C++ Compiler maxdy Using Fedora 11 26th February 2005 09:02 AM


Current GMT-time: 11:51 (Saturday, 25-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat