Re: Unsafe C++ memory allocation with new operator

From: Serassio Guido <guido.serassio@dont-contact.us>
Date: Sun, 09 Jan 2005 18:48:15 +0100

Hi Henrik,

At 23.18 08/01/2005, Henrik Nordstrom wrote:

>On Sat, 8 Jan 2005, Serassio Guido wrote:
>
>>> From my understanding of C++ the new operator is not supposed to
>>> guarantee the memory is cleared. This is the job of the constructor and
>>> related member initializers, where the default constructor memsets the
>>> object to 0.
>>
>>So, in the Robert's patch something should be wrong: he changed a
>>xcalloc() call with a new operator.
>
>No, I stand slightly corrected, there is no automatic default constructor
>in C++. Just like in C if you do not explicity initialize allocated memory
>you get garbage.

This was always clear to me. The problem, when using C++, can be to see
where is the problem, particularly when the C++ knowledge is still little
like mine ... :-(

My first time impact with C++ was the Robert's refactoring work in Squid:
non exactly a C++ implementation easy to understand for a newbe ... :-)

>There is nothing wrong with the patch. What is wrong is the
>RemovalPolicySettings class which does not have a constructor and leaves
>the object uninitialized.

Now is clear to me how fix similar problems.

> This then crashes when parsing as the parsing routines tries to free the
> old values (garbage).
>
>The following should fix it:

Yes, it works fine.

Regards

Guido

-
========================================================
Guido Serassio
Acme Consulting S.r.l. - Microsoft Certified Partner
Via Gorizia, 69 10136 - Torino - ITALY
Tel. : +39.011.3249426 Fax. : +39.011.3293665
Email: guido.serassio@acmeconsulting.it
WWW: http://www.acmeconsulting.it/
Received on Sun Jan 09 2005 - 10:48:50 MST

This archive was generated by hypermail pre-2.1.9 : Tue Feb 01 2005 - 12:00:02 MST