-
January 20th, 2013, 05:26 AM
#1
Oop
Hello , first post here
I have been studying for my programming exam lately and I have encountered some problems that I hope you could explain to me. I have been doing exams from the last years to practise and here is the portion of the code that I would love some help with:
Code:
class Sentence
{
string sent;
public:
Sentence(const string &s):s(sent)
{
cout << "Constructor Sentence: " << sent << endl;
}
void print()
{
cout << sent ;
}
~Sentece()
{
cout << "Destructor Sentence: " << sent << endl;
}
};
Code:
class Document
{
Sentence *sentences;
int n;
int max;
public:
Document(int max)
{
this->max=max;
n=0;
cout << "Constructor Document: " << max << endl;
}
void NewSentence(const string &c)
{
if(n<max)
{
Sentence s(c);
sentences[n++]=s;
}
}
void print()
{
for(int i=0;i<n;i++)
{
sentences[i].print();
}
cout << endl;
}
~Document()
{
cout << "Destructor Document: " << n << endl;
delete []sentences;
}
};
Code:
void main()
{
Document doc(10);
doc.NewSentence("abc. ");
doc.NewSentence("def. ");
doc.NewSentence("ghi. ");
doc.print();
}
In the exam, they clearly state that there is an important problem with the Document(int max) constructor and to solve the problem by doing necessary changes to it. I have no idea what's wrong with it, in fact, I compiled the program and the output was this:
Constructor Document: 10
Constructor Sentence: abc
Then, the programs shuts down for some reason. Is this related to the constructor?
Thanks for your time,
Pancake
-
January 21st, 2013, 03:43 AM
#2
Re: Oop
Originally Posted by Pancake
Is this related to the constructor?
The Document class acts as if there exists an array of Sentence (with length max) assigned to the sentences variable. This array could be allocated in the constructor but isn't.
So the sentences variable holds garbage and when its used in NewSentence some arbitrary part of memory will be overwritten and the program may crash.
-
January 21st, 2013, 08:10 AM
#3
Re: Oop
In addition to nuzzle's point about sentences being uninitialized, I'll point out that just having a Sentence pointer instead of a proper container class such as a list, and a constructor that arbitrarily constrains the number of sentences is a pretty goofy design.
-
January 22nd, 2013, 05:11 PM
#4
Re: Oop
Originally Posted by Pancake
Code:
void main()
{
Document doc(10);
doc.NewSentence("abc. ");
doc.NewSentence("def. ");
doc.NewSentence("ghi. ");
doc.print();
}
In the exam, they clearly state that there is an important problem with the Document(int max) constructor and to solve the problem by doing necessary changes to it.
Your teacher taught you that main() returns void? Get another teacher. The main() function returns an int.
I can break your class easily with a two-line main() program.
Code:
int main()
{
Document doc(10);
Document doc2 = doc;
}
You note that at the end, you're deleting the same pointer value twice, once for doc and another for doc2. This leads to undefined behaviour, possibly a crash or some sort of diagnostic. How you fix the problem -- that is for later.
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|