From owner-freebsd-hackers@FreeBSD.ORG Sat May 2 16:52:40 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 832921065715 for ; Sat, 2 May 2009 16:52:40 +0000 (UTC) (envelope-from brampton@gmail.com) Received: from mail-fx0-f162.google.com (mail-fx0-f162.google.com [209.85.220.162]) by mx1.freebsd.org (Postfix) with ESMTP id 086A48FC08 for ; Sat, 2 May 2009 16:52:34 +0000 (UTC) (envelope-from brampton@gmail.com) Received: by fxm6 with SMTP id 6so2795902fxm.43 for ; Sat, 02 May 2009 09:52:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=3SMHgXvwAI8CCEVuaYQiELw/mUUNnV0O2BxShBMakXc=; b=Bhfx6dw85oekn8+fSlbfbZduAxRBphScZcYBg8In9X44J1rQQXOyNm+rt6VZT/eG1i En+2DcTMKuS8OtfJq4nCLIFV/bbleN34mPYvRVhmav4LUZ4tVcTpdPQUnB/JoPLiVtNQ AgS3CDlSqG1DXRZcKWz4Kfn/fMLwhjpZWIx/8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=LonxkTVlZV7KpH/NuxiweuW9fv67UokChWIyAPXacNqrEh27IVMIqLS8DmQ2V0GGHJ BmSk6PH0cymZDSMYs1mJoRRalrQmMOpFtvUDXTar03tjlFeHAauA+8hyNm1f5VbLD7WM avSDZYl3hy+moQ2G8yedbTv99w7TLWmxAs+oI= MIME-Version: 1.0 Sender: brampton@gmail.com Received: by 10.223.126.69 with SMTP id b5mr1416949fas.54.1241283153704; Sat, 02 May 2009 09:52:33 -0700 (PDT) In-Reply-To: <20090502163535.GA17027@owl.midgard.homeip.net> References: <20090502163535.GA17027@owl.midgard.homeip.net> Date: Sat, 2 May 2009 17:52:33 +0100 X-Google-Sender-Auth: 138b7ed3b5492a08 Message-ID: From: Andrew Brampton To: freebsd-hackers@freebsd.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Subject: Re: Definition of NULL X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 May 2009 16:52:41 -0000 2009/5/2 Erik Trulsson : > On Sat, May 02, 2009 at 04:59:03PM +0100, Andrew Brampton wrote: >> I'm writing a C++ Kernel Module, and one thing that has been bugging >> me is the kernel's definition of NULL. > > Is the use of C++ inside the kernel really supported? =C2=A0I don't think= so, > but I could be wrong. There are a few projects (other than mine) which uses C++ inside the kernel, the biggest being the Click modular router. So, with minimal effort and no kernel patches it is easy to build a C++ kernel module, thus I must assume it is supported even if it is unofficial. The question of if C++ is sensible inside the kernel is left for another day, and perhaps has been answered in numerous other freebsd-hackers@ threads. >> >From what I've read online the definition of NULL in C is (void *)0, >> whereas in C++ it should be 0, or 0L (on 64bit machines). > > Not quite. =C2=A0Any of those (as well as a whole bunch more) are legal > definitions of NULL in C. =C2=A0NULL is defined (in the C standard) to be= a null > pointer constant. =C2=A0A null pointer constant is defined as a constant = integer > expression with value zero, or such an expression cast to (void*). (In C+= + > it the cast to (void*) is not allowed.) > This means that it would be perfectly legal (but of dubious utility) to h= ave > NULL defined as (5*5L+('1'-'0')-26) for example. > > The decision to define NULL as 0 or 0L or ((void*)0) is pretty much just = a > question of which buggy programs one wishes to break, or hide the bugs in= . > A correct C program should work regardless of which of those is used. Thanks for the additional information. > > >> >> Now, my C++ kernel module is built with _KERNEL definited, like any >> other C kernel module. This leads to NULL being defined incorrectly. >> >> So I have a question and two suggestions. Firstly, why is the #if >> defined(_KERNEL) in _null.h? Is it to stop userland application >> applications picking up this definition? Or for another reason? > > Perhaps to stop people from mistakenly using C++ inside the kernel? The definition doesn't stop C++, it just generates additional warnings and sometimes errors. I have avoided those warning by defining NULL myself, however, I thought changing it in _null.h might help others, even if you think they are mistakenly using C++. > > ... > > Erik Trulsson > ertr1013@student.uu.se > Thanks for your input. Andrew