| | |
Troubleshooting Segmentation Fault Involving Pointer
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
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:
appointment.cpp:
Thanks for taking a look.
Edit: Added in Appointment constructor.
appointment.h:
C++ Syntax (Toggle Plain Text)
class Appointment { Time startTime; Time endTime; char *subject; char *location; friend ostream &operator<<( ostream &, const Appointment & ); friend istream &operator>>( istream &, Appointment & ); public: Appointment(); Appointment(const char* subject); ~Appointment(); void read(); Appointment& operator= (const Appointment &a); bool operator== (const char *s) const; void makeAppt(); }; // class Appointment
appointment.cpp:
C++ Syntax (Toggle Plain Text)
void Appointment::makeAppt() { cout << "Subject >> "; cin >> inputAppt; } istream &operator>>( istream &input, Appointment &appt ) { input >> appt.subject; return input; } Appointment::Appointment() : subject(NULL), location(NULL) { }
Thanks for taking a look.
Edit: Added in Appointment constructor.
Last edited by dotnabox; Nov 4th, 2009 at 4:59 pm.
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#3 Nov 4th, 2009
•
•
•
•
appt.subject is a char*. What does it point at? Abandon char* and use std::string.
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.
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#5 Nov 4th, 2009
•
•
•
•
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. C++ Syntax (Toggle Plain Text)
istream &operator>>( istream &input, Appointment &appt ) { char *ptr; appt.subject = new char[strlen(ptr) + 1]; input >> appt.subject; return input; }
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; Nov 4th, 2009 at 5:18 pm.
0
#6 Nov 4th, 2009
The problem is again sort of same. . 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 . 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
C++ Syntax (Toggle Plain Text)
appt.subject = new char[strlen(ptr) + 1];
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
C++ Syntax (Toggle Plain Text)
appt.subject = new char[60];
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
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#7 Nov 4th, 2009
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?
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?
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
0
#8 Nov 4th, 2009
You've just moved the uninitialization.
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.
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.
C++ Syntax (Toggle Plain Text)
const size_t BiggestLikelyString = 2048; Appointment::Appointment() { subject = new char[BiggestLikelyString]; location = new char[BiggestLikelyString]; subject[0] = location[0] = '\0'; } Appointment::~Appointment() { delete[] subject; delete[] location; }
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
0
#10 Nov 4th, 2009
•
•
•
•
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?
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). C++ Syntax (Toggle Plain Text)
istream& operator>>(istream& is, Appointment& a) { cout << "Enter subject and location:" << endl; is >> a.subject >> a.location; return is; }
C++ Syntax (Toggle Plain Text)
istream& operator>>(istream& is, Appointment& a) { cout << "Enter subject, end with carriage return:" << endl; is.getline(a.subject, BiggestLikelyString); cout << "Enter location, end with carriage return:" << endl; is.getline(a.location, BiggestLikelyString); }
Last edited by boblied; Nov 4th, 2009 at 6:03 pm.
![]() |
Similar Threads
- mysterious segmentation fault (C)
- segmentation fault: causes. (C++)
- Segmentation fault in quick and merge sorts (C)
- Segmentation Fault (C++)
- help on 'Segmentation Fault' (C)
- segmentation fault (C)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: Some thoughts
- Next Thread: How to insert data in CLOB column
Views: 465 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






