G
Gandalf
Hello.
My code is appended at the bottom of this posting. It's a simple class
using a char* as only datamember.
The problem is the very strange result I get on Windows 98 with the latest
DevC++ 4.9.8.1 compared to Linux (Debian 3.0) g++ -version 2.95.4
So, what is the problem then? The problem is "why do I get different output
from my program?" Has there been changes in the C++ standard for handling
char* pointers or is the compiler more (stupid/smarter???)
Could someone tell me?
Result with Linux
Gandalf
Vidstige
Sauron
Sauron // as expected!
à
à // as expected.
Legolas
Legolas
Bilbo
Bilbo
Result with Windows
Gandalf
Vidstige
Sauron
Gandalf
Gandalf
Sauron
Legolas
Gandalf
Bilbo
Gandalf
----------------------------------
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class Problem {
private:
char* text;
public:
Problem() {text=new char[1]; *text='\0';}
~Problem() {delete[] text;}
void Skriv(){cout <<text<<endl;}
void Set(char* str) {delete[] text; text=new char[strlen(str)+1];
strcpy(text,str);}
};
void testref(Problem& x){
x.Set("Bilbo");
x.Skriv();
}
void test(Problem x){
x.Set("Bilbo");
x.Skriv();
}
int main() {
Problem a;
Problem b;
a.Set("Gandalf");
b.Set("Vidstige");
a.Skriv();
b.Skriv(); // everything is ok
b=a; // NOTICE THIS!
a.Set("Sauron");
a.Skriv();
b.Skriv();
test(b);
b.Skriv();
a.Skriv();
a.Set("Legolas");
a.Skriv();
b.Skriv();
testref(a);
a.Skriv();
b.Skriv();
getchar();
}
My code is appended at the bottom of this posting. It's a simple class
using a char* as only datamember.
The problem is the very strange result I get on Windows 98 with the latest
DevC++ 4.9.8.1 compared to Linux (Debian 3.0) g++ -version 2.95.4
So, what is the problem then? The problem is "why do I get different output
from my program?" Has there been changes in the C++ standard for handling
char* pointers or is the compiler more (stupid/smarter???)
Could someone tell me?
Result with Linux
Gandalf
Vidstige
Sauron
Sauron // as expected!
à
à // as expected.
Legolas
Legolas
Bilbo
Bilbo
Result with Windows
Gandalf
Vidstige
Sauron
Gandalf
Gandalf
Sauron
Legolas
Gandalf
Bilbo
Gandalf
----------------------------------
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class Problem {
private:
char* text;
public:
Problem() {text=new char[1]; *text='\0';}
~Problem() {delete[] text;}
void Skriv(){cout <<text<<endl;}
void Set(char* str) {delete[] text; text=new char[strlen(str)+1];
strcpy(text,str);}
};
void testref(Problem& x){
x.Set("Bilbo");
x.Skriv();
}
void test(Problem x){
x.Set("Bilbo");
x.Skriv();
}
int main() {
Problem a;
Problem b;
a.Set("Gandalf");
b.Set("Vidstige");
a.Skriv();
b.Skriv(); // everything is ok
b=a; // NOTICE THIS!
a.Set("Sauron");
a.Skriv();
b.Skriv();
test(b);
b.Skriv();
a.Skriv();
a.Set("Legolas");
a.Skriv();
b.Skriv();
testref(a);
a.Skriv();
b.Skriv();
getchar();
}