Title: | C++ |
Notice: | Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS) |
Moderator: | DECCXX::AMARTIN |
Created: | Fri Nov 06 1987 |
Last Modified: | Thu Jun 05 1997 |
Last Successful Update: | Fri Jun 06 1997 |
Number of topics: | 3604 |
Total number of notes: | 18242 |
Hi, We are running openvms 7.1 and using dec c++ 5.5. We are using the Queue classes(qhead & qtail classes) provided under the task.hxx header file. Our problem is to create a queue so that we can do a normal operations like Insertions and Deletions on the queue. we are adding the data at the tail part using qtail::put() function & trying to remove the data at the head part using qhead::get(). When we insert an element in the queue and then dequeue it using get, its fine. but when we insert an element again using qtail::put(),it gives us an access violation. If we are queueing up 10 elements (equal to the maximum size of the queue) using put() and then dequeue 10 elements using get() and again try to queue up even one element it gives an access violation. Are we missing something? we are using Open VMS v7.1 with DEC C++ compiler v5.5 The following is the source code : #include <iostream.h> #include <task.hxx> class str : public object { char *s; public: str(char *p) // constructor with one arguments { p = new char[strlen(s)]; strcpy(p,s); } ~str() { delete s; } // destructor void prnt() // print the deleted element { cout << " Removed entry : " << s << endl; } }; void main() { qtail qt(WMODE,10); // qt is object of type qtail qhead qh; // qh is object of type qhead str a("hello"); str b("queue"); str c("cpp"); str *a1,*a2,*a3; a1 = &a; a2 = &b; a3 = &c; // set the maximun size of queue qt.setmax(10); cout << " Before insering :: Free slots : " << qt.rdspace() << "Enries : " << qh.rdcount() << endl; // inserting the element into the queue if(!qt.put(a1)) cout << " error in iserting 1 " << endl; qh = *((qhead*) qt.head()); cout << " Before insering :: Free slots : " << qt.rdspace() << "Enries : " << qh.rdcount() << endl; // inserting the second element if(!qt.put(a2)) cout << " error in iserting 1 " << endl; qh = *((qhead*) qt.head()); cout << " Before insering :: Free slots : " << qt.rdspace() << "Enries : " << qh.rdcount() << endl; // deleting the element from the queue a1 = (str*) qh.get(); a1 -> prnt(); // printing the element qt = *((qtail *) qh.tail()); cout << " Before insering :: Free slots : " << qt.rdspace() << "Enries : " << qh.rdcount() << endl; // delete the second element a2 = (str*) qh.get(); a2 -> prnt(); // printing the element qt = *((qtail *) qh.tail()); cout << " Before insering :: Free slots : " << qt.rdspace() << "Enries : " << qh.rdcount() << endl; // inserting third element // problem with inserting the element into the queue if(!qt.put(a3)) cout << " error in iserting 1 " << endl; qh = *((qhead*) qt.head()); cout << " Before insering :: Free slots : " << qt.rdspace() << "Enries : " << qh.rdcount() << endl; // delete the third element a3 = (str*) qh.get(); a3 -> prnt(); // printing the element qt = *((qtail *) qh.tail()); cout << " Before insering :: Free slots : " << qt.rdspace() << "Enries : " << qh.rdcount() << endl; }
T.R | Title | User | Personal Name | Date | Lines |
---|---|---|---|---|---|
3589.1 | first thing to try | SPECXN::DERAMO | Dan D'Eramo | Tue May 27 1997 11:01 | 11 |
> str(char *p) // constructor with one arguments > { > p = new char[strlen(s)]; > strcpy(p,s); > } There is a memory corrupter there; you need to 'new' strlen(s)+1 characters in order to hold the terminating '\0' byte. Dan | |||||
3589.2 | Here's my theory as to what's going wrong... | DECC::J_WARD | Wed May 28 1997 14:50 | 137 | |
Hi, I think the problem has to do with how the qtail and qhead classes were designed (or misdesigned (:). Let me explain. A particular queue consists of a qtail object and a qhead object. There is a one-to-one correspondence between these two objects, i.e. a qhead contains a pointer to the qtail and the qtail contains a pointer to the qhead. Now let's see what you're trying to do with code like: qh = *((qhead*) qt.head()); You're trying to assign a qhead object to another qhead object. I don't see how this could ever work. If the user is allowed to assign or copy qhead/qtail objects into other qhead/qtail objects then there would be no way to maintain the one-to-one correspondence between a qhead and a qtail. I actually think the library should have declared a private assignment operator and copy constructor for qhead and qtail to prevent users from attempting something like this since I don't think (without redesigning the whole class) that these functions could ever be written to work as the user expects they should. So I think your code should be changed to something like the code below.. (I also fixed up the str class.) Judy #include <iostream.h> #include <task.hxx> class str : public object { char *s; public: str(char *p) // constructor with one arguments { s = new char[strlen(p)+1]; strcpy(s,p); } ~str() { delete [] s; } // destructor void prnt() // print the deleted element { cout << " Removed entry : " << s << endl; } }; void main() { qtail qt(WMODE,10); // qt is object of type qtail qhead qh; // qh is object of type qhead qtail* qtp=&qt; qhead* qhp=&qh; str a("hello"); str b("queue"); str c("cpp"); str *a1,*a2,*a3; a1 = &a; a2 = &b; a3 = &c; // set the maximun size of queue qtp->setmax(10); cout << " Before inserting :: Free slots : " << qtp->rdspace() << " Entries : " << qhp->rdcount() << endl; // inserting the element into the queue if(!qtp->put(a1)) cout << " error in iserting 1 " << endl; qhp = qtp->head(); cout << " Before inserting :: Free slots : " << qtp->rdspace() << " Entries : " << qhp->rdcount() << endl; // inserting the second element if(!qtp->put(a2)) cout << " error in iserting 1 " << endl; qhp = qtp->head(); cout << " Before inserting :: Free slots : " << qtp->rdspace() << " Entries : " << qhp->rdcount() << endl; // deleting the element from the queue a1 = (str*) qhp->get(); a1 -> prnt(); // printing the element qtp = qhp->tail(); cout << " Before inserting :: Free slots : " << qtp->rdspace() << " Entries : " << qhp->rdcount() << endl; // delete the second element a2 = (str*) qhp->get(); a2 -> prnt(); // printing the element qtp = qhp->tail(); cout << " Before inserting :: Free slots : " << qtp->rdspace() << " Entries : " << qhp->rdcount() << endl; // inserting third element // problem with inserting the element into the queue if(!qtp->put(a3)) cout << " error in iserting 1 " << endl; qhp = qtp->head(); cout << " Before inserting :: Free slots : " << qtp->rdspace() << " Entries : " << qhp->rdcount() << endl; // delete the third element a3 = (str*) qhp->get(); a3 -> prnt(); // printing the element qtp = qhp->tail(); cout << " Before inserting :: Free slots : " << qtp->rdspace() << " Entries : " << qhp->rdcount() << endl; } |