From: White Wolf on
n00m wrote:
> On Nov 27, 7:45 pm, Ulrich Eckhardt <dooms...(a)knuut.de> wrote:
>> What made you suspect it could be wrong at all? What didn't work as you
>> expected?
>
> Just came across a post (in other place) of an applicant to a job.
> He got some tests (and failed to pass them). The 1st task was:
> point out all "problems" in the code (see below):
>
>
> class Foo
> {
> public:
> Foo(int j) { i = new int[j]; }
> ~Foo() { delete i; }
> private:
> int* i;
> };
>
> class Bar: Foo
> {
> public:
> Bar(int j) { i = new char[j]; }
> ~Bar() { delete i; }
> private:
> char* i;
> };
>
>
> void main()
> {
> Foo* f = new Foo(100);
> Foo* b = new Bar(200);
> *f = *b;
> delete f;
> delete b;
> }

This is described in detail in all C++ books, including but not limited
to Effective C++.

Attila
From: Francis Glassborow on
White Wolf wrote:
> n00m wrote:
>> On Nov 27, 7:45 pm, Ulrich Eckhardt <dooms...(a)knuut.de> wrote:
>>> What made you suspect it could be wrong at all? What didn't work as you
>>> expected?
>>
>> Just came across a post (in other place) of an applicant to a job.
>> He got some tests (and failed to pass them). The 1st task was:
>> point out all "problems" in the code (see below):
>>
>>
>> class Foo
>> {
>> public:
>> Foo(int j) { i = new int[j]; }
>> ~Foo() { delete i; }
>> private:
>> int* i;
>> };
>>
>> class Bar: Foo
>> {
>> public:
>> Bar(int j) { i = new char[j]; }
>> ~Bar() { delete i; }
>> private:
>> char* i;
>> };
>>
>>
>> void main()
>> {
>> Foo* f = new Foo(100);
>> Foo* b = new Bar(200);
>> *f = *b;
>> delete f;
>> delete b;
>> }
>
> This is described in detail in all C++ books, including but not limited
> to Effective C++.
>
> Attila

But the problem is not the use of a delete operator in a class dtor but
the wrong one. Actually there are quite a number of other errors and
examples of poor style.

The naming is bad (though not an error in itself, it makes the code
harder to read).
We have a data member of a derived class with the same name as one in
the base class (again, not an error in itself)

The bar ctor does not initialise its base (and so, in this case, should
fail as there is no default ctor)

Had the coder used ctor-init lists instead of leaving the initialisation
of the i to the body of the ctor he would have been less likely to
forget the rest of the init. requirements.

Foo was probably not meant to be a private base of Bar

Then we have the minor irritant of main returning void

Next look at *f = *b. That asks us to copy an unnamed Bar object to an
unnamed Foo object. As it is we need not bother because you cannot
create a Bar object but if you could ...

Were the copying successful you would just have leaked memory when you
delete f. However deleting b may or not cause problems depending on how
you fix the construction of Bar instances.

Granted all the problems are covered in most introductory C textbooks
but identifying them in code is not quite the same thing.

Supposing that Bar is fixed so that instances can be constructed, it
takes more than what a novice normally learns to identify the behaviour
of *f = *b.


From: White Wolf on
Francis Glassborow wrote:
> White Wolf wrote:
>> n00m wrote:
>>> On Nov 27, 7:45 pm, Ulrich Eckhardt <dooms...(a)knuut.de> wrote:
>>>> What made you suspect it could be wrong at all? What didn't work as you
>>>> expected?
>>>
>>> Just came across a post (in other place) of an applicant to a job.
>>> He got some tests (and failed to pass them). The 1st task was:
>>> point out all "problems" in the code (see below):
>>>
>>>
>>> class Foo
>>> {
>>> public:
>>> Foo(int j) { i = new int[j]; }
>>> ~Foo() { delete i; }
>>> private:
>>> int* i;
>>> };
>>>
>>> class Bar: Foo
>>> {
>>> public:
>>> Bar(int j) { i = new char[j]; }
>>> ~Bar() { delete i; }
>>> private:
>>> char* i;
>>> };
>>>
>>>
>>> void main()
>>> {
>>> Foo* f = new Foo(100);
>>> Foo* b = new Bar(200);
>>> *f = *b;
>>> delete f;
>>> delete b;
>>> }
>>
>> This is described in detail in all C++ books, including but not
>> limited to Effective C++.
>
> But the problem is not the use of a delete operator in a class dtor but
> the wrong one. Actually there are quite a number of other errors and
> examples of poor style.
[SNIP]

Exactly. :-) And Effective C++ covers all those topics, in an easy to
understand, easy to access (PDF version available), easy to read format.
And I guess so does the FAQ... :)

--
BR, WW
From: n00m on
On Nov 27, 11:09 pm, LR <lr...(a)superlink.net> wrote:
> Regarding your question about delete,
>
> http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.12http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.13http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.14
>
> LR

nice links