Yet another Segmentation Fault (Debugging 2)
Points : 20
Remove error(s)/segmentation fault(s)/warning(s) from the given CPP code by
- Deleting exactly three characters (7 marks)
- Adding exactly one keyword (5 marks)
In addition to above changes by further deleting exactly three characters (8 marks), change c1 contents such that
c1.id[0]=c1.id[1]=c1.id[2]=0;
c1.id[3]=200;
For all the changes you do, you need to provide proper explanation for the same. More than the answer, explanation carries more points in your solution.
Note: Use g++ 3.4 or higher compiler. Avoid using Turbo compilers. Do not use any optional flag for compilation.
Code
- #include<iostream>
- #include<limits>
- using namespace std;
- class Codefest {
- int a;
- int id[20000];
- public:
- Codefest();
- Codefest(int);
- int& eventsCount();
- void print();
- int& operator[](int);
- Codefest& operator=(const Codefest&);
- };
- Codefest::Codefest(){
- a=0;
- for(int i=0;i<20000;i++)
- id[i]=0;
- }
- Codefest::Codefest(int x){
- a=x;
- for(int i=0;i<20000;i++)
- id[i]=0;
- }
- int& Codefest::operator[](int x){
- return id[x];
- }
- Codefest& Codefest::operator=(const Codefest& c){
- a=c.a;
- for(int i=0;i<20000;i++){
- id[i]=c.id[i];
- }
- }
- int& Codefest::eventsCount(){
- int ec=6;
- return ec;
- }
- void Codefest::print(){
- cout<<"Hello CodeWarriors"<<a<<endl;
- }
- int main(){
- Codefest* p2010 = NULL;
- p2010->print();
- p2010->eventsCount();
- Codefest c1;
- c1[0]=3;
- c1[1]=90;
- c1[2]=100;
- c1[3]=200;
- for(int i=0;i<4;i++){
- cout<<c1[i]<<endl;
- }
- return 0;
- }
#include<iostream>
#include<limits>
using namespace std;
class Codefest {
int a;
int id[20000];
public:
Codefest();
Codefest(int);
int& eventsCount();
void print();
int& operator[](int);
Codefest& operator=(const Codefest&);
};
Codefest::Codefest(){
a=0;
for(int i=0;i<20000;i++)
id[i]=0;
}
Codefest::Codefest(int x){
a=x;
for(int i=0;i<20000;i++)
id[i]=0;
}
int& Codefest::operator[](int x){
return id[x];
}
Codefest& Codefest::operator=(const Codefest& c){
a=c.a;
for(int i=0;i<20000;i++){
id[i]=c.id[i];
}
}
int& Codefest::eventsCount(){
int ec=6;
return ec;
}
void Codefest::print(){
cout<<"Hello CodeWarriors"<<a<<endl;
}
int main(){
Codefest* p2010 = NULL;
p2010->print();
p2010->eventsCount();
Codefest c1;
c1[0]=3;
c1[1]=90;
c1[2]=100;
c1[3]=200;
for(int i=0;i<4;i++){
cout<<c1[i]<<endl;
}
return 0;
}
Solution
- To remove the warning, add a keyword static to line 39.
static int ec = 6
This is because whenever you are returning a reference you must be sure that the associated object exists even after returning from the function. Since locals get destroyed immediately after exit from function the returned reference must point to a static area which exists till the end of the program.
Reference: Pg 452 Bruce Eckel Thinking in C++ Vol 1. - To remove the run time error/segmentation fault, delete 3 characters from line 43.
Though a "this" pointer is passed to every member function, that is not used until a member variable is accessed. Removing variable "a" from the line eliminates the null "this" pointer dereference , which was the reason for segmentation fault.cout<<"Hello Warriors "<<endl;
Reference: Pg 240 Bruce Eckel Thinking in C++ Vol 1 - To get the desired output, change line 53 to
c1 = 100
This is an example of type conversion where the line has the same effect as
c1 = Codefest(100);
Constructors with single arguments also serve as type conversions. After this the "operator=" is invoked for the assignment and hence the new contents.
Reference: http://www.cplusplus.com/doc/tutorial/typecasting/Post your queries regarding the solution at Debugging 2 Forum Thread.
Submit your solution
You need to be logged in to submit a solution.
discussions
| Sid | regarding ; |
| Q: | ; is counted as a character |
| A: | Yes. View http://www.asciitable.com/ for the list of all 256 characters. |
| Sid | regarding variable |
| Q: | can we replace some character along with deleting exactly 3 chracters |
| A: | No, you are allowed to only delete 3 characters. |
| ReDucTor | query |
| Q: | Does keyword include opperand (e.g. new xxx) |
| A: | Yes it does. Refer http://www.cppreference.com/wiki/keywords/start for list of all CPP keywords. |
| xolve | What counts as character? |
| Q: | Do spaces and ,=)(}{[] count as characters to delete? |
| A: | Yes. View http://www.asciitable.com/ for the list of all 256 characters. |
| codegambler | query |
| Q: | can i shift the position of lines in code. |
| A: | No. Do only as mentioned in the problem statement |
| vivekn | No of chars |
| Q: | Should i delete 3 chars from existing code and change nothing else or should there be a net decrease of 3 chars in the code? |
| A: | You are allowed to only delete 3 character from existing code and add a keyword. |
post a question
Loading...
