Mail Archives: cygwin/2002/08/05/14:29:56
On Mon, 5 Aug 2002 07:55:56 -0700 (PDT), ejfried AT ca DOT sandia DOT gov
(friedman_hill ernest j) wrote:
>Arash,
>
>If your program runs on a bunch of systems, well, you're just lucky,
>because the bugs are in your code, not in cygwin. You've provided a
>constructor (whitespace edited to make this email shorter)
>
>DigitList::DigitList(DigitList const &diglst) {
> *this = diglst;
>};
>
>which just invokes, implicitly, the default copy constructor.
Default assignment operator? ;-)
>This
>default copy constructor would look something like this:
>
>DigitList::DigitList(DigitList &diglst) {
> this.digitList = diglist.digitList; // *** This line
> this.listSize = diglist.listSize;
>}
>
>The line I've marked with a *** is the trouble maker -- it makes an
>alias for the pointer member digitList (which is effectively an
>int*). When you copy a DigitList using this copy ctor, you get two
>DigitLists sharing a single digitList pointer.
>
>Now, in your dtor:
>
>DigitList::~DigitList() {
> listSize = 0;
> free(digitList);
>};
>
>you free digitList. But if two objects sharing a digitList both delete
>the same pointer, then you've crossed over into the realm of undefined
>behaviour. The implementation is allowed to do anything now: it can
>keep running without a problem, it can crash (as cygwin's newlib is
>apparently doing) or it can send dirty pictures of itself to the
>White House.
>
>Anyway, so you should THANK cygwin for finding the bugs in your
>code. BTW, 1) why are you using free and malloc instead of new and
>delete, anyway? and 2) Why are you writing a class like DigitList in the
>first place -- why not simply use a vector<int>? and 3) I've only
>pointed out ONE of your memory management bugs. There are plenty more
>in this code.
[snip]
--
swamp-dog AT ntlworld DOT com
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -