PetscTruth bool?

Boyce Griffith griffith at cims.nyu.edu
Wed Sep 24 12:23:34 CDT 2008


Hi, Barry et al. --

I may be mistaken, but I believe that sizeof(bool) is almost always one 
bit in C++.  This results in various problems, including the need to 
have a specialized implementation of std::vector<bool> in the STL.  See, 
e.g.,

    http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98

-- Boyce

Barry Smith wrote:
> 
>    Interesting, they don't mind totally screwing up memory alignments in 
> order to save some
> space on a bool. (I mean, come on, who makes an array of a million bools 
> anyways?)
> 
>     From this, if we did switch to bool it would totally screw up our 
> Fortran interface (Fortran does
> use integer for logical).
> 
>    So forget what I suggested, keep putting the ugly (PetscTruth) casts 
> in argument lists
> all over PETSc because the C++ compiler stubbornly refuses to auto-cast 
> a bool to an enum
> and we'll leave it the way it is now.
> 
>     Barry
> 
> On Sep 24, 2008, at 11:27 AM, Lisandro Dalcin wrote:
> 
>> Barry, see this
>>
>> $ cat testbool.c
>> #include <stdio.h>
>> #include <stdbool.h>
>> int main(){
>>  printf("sizeof(bool): %d\n", sizeof(bool));
>>  printf("sizeof(int):  %d\n", sizeof(int));
>> }
>> $ gcc testbool.c
>> $ a.out
>> sizeof(bool): 1
>> sizeof(int):  4
>>
>> And now this:
>>
>> $ cat testbool.cpp
>> #include <iostream>
>> int main(){
>>  std::cout << "sizeof(bool): " << sizeof(bool) << std::endl;
>>  std::cout << "sizeof(int): "  << sizeof(int)  << std::endl;
>> }
>> $ g++ testbool.cpp
>> $ a.out
>> sizeof(bool): 1
>> sizeof(int): 4
>>
>> Unless I'm doing something wrong, it seems that in my system (32bits
>> box, FedoraCore 6) 'bool' do not  have the size of an 'int' in both C
>> and C++.
>>
>>
>> On Wed, Sep 24, 2008 at 12:59 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:
>>>
>>> On Sep 24, 2008, at 9:30 AM, Matthew Knepley wrote:
>>>
>>>> On Wed, Sep 24, 2008 at 8:51 AM, Barry Smith <bsmith at mcs.anl.gov> 
>>>> wrote:
>>>>>
>>>>> Since C++ has bool and C99 has bool, should PetscTruth be changed 
>>>>> to be
>>>>> defined to be bool? Instead of int.
>>>>
>>>> I see the motivation, but we would have to recode any MPI usages of
>>>> PetscTruth
>>>> since we could not guarantee the size of bool.
>>>
>>>  Matt,
>>>
>>>   YOU, of all people, being pedantic :-)
>>>
>>>   Show me a single system where a bool is not an int size.
>>>
>>>  Actually, some Fortran compilers use a -1 as true (not a 1!), this is
>>> already
>>> painful and could be tricky.
>>>
>>>
>>>  Barry
>>>
>>>
>>>>
>>>>
>>>> Matt
>>>>
>>>>> Barry
>>>>>
>>>>> Should we go further and simply remove PetscTruth and use bool 
>>>>> directly
>>>>> in
>>>>> its place? (logical in Fortran).
>>>>
>>>> -- 
>>>> What most experimenters take for granted before they begin their
>>>> experiments is infinitely more interesting than any results to which
>>>> their experiments lead.
>>>> -- Norbert Wiener
>>>>
>>>
>>>
>>
>>
>>
>> -- 
>> Lisandro Dalcín
>> ---------------
>> Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
>> Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
>> Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
>> PTLC - Güemes 3450, (3000) Santa Fe, Argentina
>> Tel/Fax: +54-(0)342-451.1594
>>
> 




More information about the petsc-dev mailing list