Re: cast pid_t to int when using *printf

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Oliver Jowett <oliver(at)opencloud(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, Neil Conway <neilc(at)samurai(dot)com>, pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: cast pid_t to int when using *printf
Date: 2004-10-09 02:51:19
Message-ID: 200410090251.i992pJI05666@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

Oliver Jowett wrote:
> Peter Eisentraut wrote:
> > Am Freitag, 24. September 2004 09:34 schrieb Oliver Jowett:
> >
> >>Neil Conway wrote:
> >>
> >>>On Fri, 2004-09-24 at 16:51, Oliver Jowett wrote:
> >>>
> >>>>gcc (3.2.3 on Solaris 9) warns about a couple of places where a pid_t is
> >>>>formatted with %d by a printf-family function.
> >>>
> >>>For curiosity's sake, what formatting escape does gcc prefer?
> >>
> >>I don't think there is an escape for pid_t, you always have to cast it.
> >
> >
> > I think what he was asking is this: Since pid_t has to be a signed integer
> > type, but gcc does not accept %d for it, then it could be that pid_t is wider
> > than an int, so casting it to int would potentially lose information.
>
> pid_t on the Solaris/sparc system is a long (but both int and long are
> 32 bits). Some experimentation shows that gcc is happy with a %ld format
> specifier. But compiling the same code on a Linux/x86 system makes gcc
> complain when applying %ld to pid_t, so we will need a cast there either
> way.
>
> I notice that elog.c casts MyProcPid to long and uses %ld. Amusingly,
> MyProcPid is explicitly an int..

I see in include/sys/types.h on Solaris 9:

#if defined(_LP64) || defined(_I32LPx)
typedef uint_t nlink_t; /* file link type */
typedef int pid_t; /* process id type */
#else
typedef ulong_t nlink_t; /* (historical version) */
typedef long pid_t; /* (historical version) */
#endif

I am confused why you are seeing long for pid_t? What is your Solaris
system information? If Solaris needs adjustments, there are a lot of
place we print getpid().

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

In response to

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2004-10-09 03:24:47 Re: patch for temporary view from TODO list
Previous Message Bruce Momjian 2004-10-09 02:42:47 Re: cast pid_t to int when using *printf