[petsc-dev] need C++ expert help

Munson, Todd tmunson at mcs.anl.gov
Tue Sep 13 15:05:36 CDT 2016


The write statement return type is ssize_t.  For whatever reason its a signed type
because it can return -1 on error.

I would suggest using the ssize_t type for both and changing the "!=" to a "<"
which will abort if write either returns a negative or the number of bytes 
written is not correct.

Note: you will have problems if (ssize_t) (m*n*sizeof(double)) is negative 
though, but that can be checked as well.

Todd.

> On Sep 13, 2016, at 2:50 PM, Satish Balay <balay at mcs.anl.gov> wrote:
> 
> This works..
> 
> Satish
> 
> ---------
> 
> diff --git a/src/mat/examples/tests/ex45.c b/src/mat/examples/tests/ex45.c
> index 63d0a30..37f68e3 100644
> --- a/src/mat/examples/tests/ex45.c
> +++ b/src/mat/examples/tests/ex45.c
> @@ -72,7 +72,7 @@ void Store2DArray(int m,int n,double *a,const char *filename,int *fdd)
>       vals[i+m*j] = a[j+i*n];
>     }
>   }
> -  if (write(fd,vals,m*n*sizeof(double)) != ((unsigned int) (m*n))*sizeof(double)) abort();
> +  if ((size_t)write(fd,vals,m*n*sizeof(double)) != (size_t)(m*n*sizeof(double))) abort();
>   free(vals);
> 
> }
> @@ -94,7 +94,7 @@ void Store1DArray(int m,double *a,const char *filename,int *fdd)
>   }
>   if (write(fd,&classid,sizeof(int)) != sizeof(int)) abort();
>   if (write(fd,&m,sizeof(int)) != sizeof(int)) abort();
> -  if (write(fd,a,m*sizeof(double)) != ((unsigned int)m)*sizeof(double)) abort();
> +  if ((size_t)write(fd,a,m*sizeof(double)) != (size_t)(m*sizeof(double))) abort();
> }
> 
> 
> On Tue, 13 Sep 2016, Satish Balay wrote:
> 
>> I would tink if its typecast to same type - comparision would work..
>> 
>> if ((ssize_t)write(fd,vals,m*n*sizeof(double)) != (ssize_t)(m*n*sizeof(double))) abort();
>> 
>> I'll give this a try...
>> 
>> Satish
>> 
>> On Tue, 13 Sep 2016, Barry Smith wrote:
>> 
>>> 
>>>   I need help from someone who knows the subtleties of c++.  We have a PETSc example that always compiles fine with C but generates annoying warnings with C++. I've tried many different variants to eliminate the warnings with no luck
>>> 
>>>   Any help is appreciated.
>>> 
>>>    Barry
>>> 
>>> 
>>> mpicxx -o ex45.o -c -Wall -Wwrite-strings -Wno-strict-aliasing -Wno-unknown-pragmas -fvisibility=hidden -g -O   -fPIC    -I/sandbox/petsc/petsc.clone-3/include -I/sandbox/petsc/petsc.clone-3/arch-linux-opt-cxx-quad/include -I/usr/include/mpich2    `pwd`/ex45.c
>>> /sandbox/petsc/petsc.clone-3/src/mat/examples/tests/ex45.c: In function ‘void Store2DArray(int, int, double*, const char*, int*)’:
>>> /sandbox/petsc/petsc.clone-3/src/mat/examples/tests/ex45.c:75:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
>>>   if (write(fd,vals,m*n*sizeof(double)) != ((unsigned int) (m*n))*sizeof(double)) abort();
>>>                                         ^
>>> /sandbox/petsc/petsc.clone-3/src/mat/examples/tests/ex45.c: In function ‘void Store1DArray(int, double*, const char*, int*)’:
>>> /sandbox/petsc/petsc.clone-3/src/mat/examples/tests/ex45.c:97:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
>>>   if (write(fd,a,m*sizeof(double)) != ((unsigned int)m)*sizeof(double)) abort();
>>> 
>>> 
>>> It can be found here ftp://ftp.mcs.anl.gov/pub/petsc/nightlylogs/archive/2016/09/13/examples_next_arch-linux-opt-cxx-quad_grind.log
>> 



More information about the petsc-dev mailing list