Troubleshooting Segmentation Fault Involving Pointer

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster

Troubleshooting Segmentation Fault Involving Pointer

 
0
  #1
34 Days Ago
Or at least I think that's the issue. Essentially, I am in the process of overloading operators and allowing users to enter data directly into the class objects. I was able to do the first one so I followed the same pattern and I get a segmentation fault when I try to cin >> the information. Everything else about the program operates as it should, so I'll post what I think are the relevant pieces of code:

appointment.h:
  1. class Appointment
  2. {
  3. Time startTime;
  4. Time endTime;
  5. char *subject;
  6. char *location;
  7. friend ostream &operator<<( ostream &, const Appointment & );
  8. friend istream &operator>>( istream &, Appointment & );
  9. public:
  10. Appointment();
  11. Appointment(const char* subject);
  12. ~Appointment();
  13. void read();
  14. Appointment& operator= (const Appointment &a);
  15. bool operator== (const char *s) const;
  16. void makeAppt();
  17. }; // class Appointment

appointment.cpp:
  1. void Appointment::makeAppt()
  2. {
  3. cout << "Subject >> ";
  4. cin >> inputAppt;
  5. }
  6.  
  7. istream &operator>>( istream &input, Appointment &appt )
  8. {
  9. input >> appt.subject;
  10. return input;
  11. }
  12.  
  13. Appointment::Appointment() : subject(NULL), location(NULL)
  14. {
  15. }

Thanks for taking a look.

Edit: Added in Appointment constructor.
Last edited by dotnabox; 34 Days Ago at 4:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster
 
0
  #2
34 Days Ago
appt.subject is a char*. What does it point at? Abandon char* and use std::string.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster
 
0
  #3
34 Days Ago
Originally Posted by boblied View Post
appt.subject is a char*. What does it point at? Abandon char* and use std::string.
Honestly, pointers are my weak point. We use the professor's code from the previous assignment and build off of it. Next week we can use string, finally, so there will be less of this.

In terms of what is points to, I'm not entirely sure to be honest. I've been starting at my code for a while now and I'm confused at the char *subject and char *location in the Appointment class.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #4
34 Days Ago
Its happening because subject is not pointing to a valid address, basically you haven't allocated memory to it. Either use char subject[size] or allocate memory in constructor using malloc() or new operator.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster
 
0
  #5
34 Days Ago
Originally Posted by vishesh View Post
Its happening because subject is not pointing to a valid address, basically you haven't allocated memory to it. Either use char subject[size] or allocate memory in constructor using malloc() or new operator.
Using code similar to other references to subject in Appointment, I tried this:
  1. istream &operator>>( istream &input, Appointment &appt )
  2. {
  3. char *ptr;
  4. appt.subject = new char[strlen(ptr) + 1];
  5.  
  6. input >> appt.subject;
  7. return input;
  8. }
It still compiles and segfaults, although now earlier.

Edit: It does work, however, with appt.subject = new char[integer value]. I know he'll want dynamic allocation, though.

How would I dynamically allocate memory to it properly?
Last edited by dotnabox; 34 Days Ago at 5:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #6
34 Days Ago
The problem is again sort of same.
  1. appt.subject = new char[strlen(ptr) + 1];
. Neither ptr holds any string nor it point to any dynamically allocated memory and you are still trying to calculate its length using strlen!

We could only guess what will be size of user input(unless you take it from user in runtime) and according to that we give some memory to it prior taking input.

So you have two options:
1. Either guess
  1. appt.subject = new char[60];
. 60 is usually more than enough for this variable I suppose. However unless you have reasons, statically allocating memory is preferable.

2. Or ask user using cin, the size of subject and then allocate that much......foolish approach but would satisfy your professor if he needs use of dynamic memory allocation
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster
 
0
  #7
34 Days Ago
Thanks. I think I'll just stick with setting it to 60, at least for now. Once I finish the project, I'll go back and take another serious look at it. So consider the initial problem solved, then. I have quickly run into another snag, though, if you have another moment to spare.

I need to read in both sobject and location from this, but I'm really not sure how to go about it. With that code, calling the >> operator will always store it into appt.subject, but what about storing information to appt.location? Any suggestions on an approach?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster
 
0
  #8
34 Days Ago
You've just moved the uninitialization. ptr is a random number.

ptr, subject, and location are just 4 tiny little bytes that are the address of some other memory. If you leave them uninitialized or set to NULL, then they're pointing at nothing, or at least nothing useful. They have to become the address of something, or the program will blow up when trying to use them.

This is the basic problem with char*. You have to guess what the maximum size might be and allocate enough space. If you guess wrong, then hackers overflow your buffers and steal your bank account. If you don't guess at all, your program crashes.

To solve this problem, in the constructor of Appointment, allocate space for subject and location, and recover it in the destructor.
  1. const size_t BiggestLikelyString = 2048;
  2. Appointment::Appointment()
  3. {
  4. subject = new char[BiggestLikelyString];
  5. location = new char[BiggestLikelyString];
  6. subject[0] = location[0] = '\0';
  7. }
  8. Appointment::~Appointment()
  9. {
  10. delete[] subject;
  11. delete[] location;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster
 
0
  #9
34 Days Ago
@boblied: I do like that a lot better than what I currently have. I think when I go through and revise it after I get it to all function properly I'll switch over to that. Like I said, though, this will become less of an issue once we are allowed to use strings next week.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster
 
0
  #10
34 Days Ago
Originally Posted by dotnabox View Post
I need to read in both sobject and location from this, but I'm really not sure how to go about it. With that code, calling the >> operator will always store it into appt.subject, but what about storing information to appt.location? Any suggestions on an approach?
Each time you use operator>>() , it will read one word. For the purposes of the exercise, you could assume that subject will be one word and location will be another word. If either one could be a line of multiple words, you will have to use the getline() function (see standard library documentation).
  1. istream& operator>>(istream& is, Appointment& a)
  2. {
  3. cout << "Enter subject and location:" << endl;
  4. is >> a.subject >> a.location;
  5. return is;
  6. }
or
  1. istream& operator>>(istream& is, Appointment& a)
  2. {
  3. cout << "Enter subject, end with carriage return:" << endl;
  4. is.getline(a.subject, BiggestLikelyString);
  5. cout << "Enter location, end with carriage return:" << endl;
  6. is.getline(a.location, BiggestLikelyString);
  7. }
Last edited by boblied; 34 Days Ago at 6:03 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC