Article View: sublink.comp.unix
Article #1Return value from shmat(2) is not portable ?
From: Ugo Cei
Date: Mon, 06 May 1991 18:37
Date: Mon, 06 May 1991 18:37
58 lines
2685 bytes
2685 bytes
[This is crossposted to comp.lang.c, since the discussion on the pointer/integer relationship may have a wider audience than just Unix programmers.] From a typical SysV shmat(2) man page: | char *shmat (shmid, shmaddr, shmflg) | int shmid; | char *shmaddr; | int shmflg; [...] | Diagnostics | Upon successful completion, the return value is as follows: | | shmat returns the data segment start address of the | attached shared memory segment. | | shmdt returns a value of 0. | | Otherwise, a value of -1 is returned, and errno is set to | indicate the error. I have always believed that comparing pointers to integers was not portable, unless the integer is the constant 0 (which is not an integer at all, in this context). Well, if you concede me this, how am I supposed to test for the failure or success of shmat(2) ? Maybe: char * shmaddr; if((shmaddr = shmat(shmid, shmaddr, shmflg)) < 0) ... This, however, elicits a severe warning from my compiler, since I am not supposed to do an ordered comparison of a pointer and an integer. This is an issue that I wholeheartedly agree with. In the end, I have resorted to this kind of kludge: if((shmaddr = shmat(shmid, shmaddr, shmflg)) == (char *)(~0)) ... I know, this is not portable either, but my eyes are much more pleased since this resembles ``... == (char *) 0'', which IS portable. And even my compiler has stopped complaining. Now, my question is: why on earth wasn't shmat(2) made to return 0 (NULL) in case of failure ? Are there some subtle reasons for this that I am not aware of ? Inquiring mind wants to know. -- **************** | Ugo Cei | home: newsuser@oliver.sublink.org * OLIVER * | Via Colombo 7 | office: cei@ipvvis.unipv.it **************** | 27100 Pavia ITALY | "Real Programs Dump Core"
Technical Details
Message-ID:
<986@oliver.SUBLINK.ORG>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!uwm.edu!rutgers!modus!simm!yachaya!oliver!newsuserShow Full Headers
Complete Article Headers
Message-ID: <986@oliver.SUBLINK.ORG> Subject: Return value from shmat(2) is not portable ? Date: Mon, 06 May 1991 18:37:15 +0000 From: newsuser@oliver.SUBLINK.ORG (Ugo Cei) Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!uwm.edu!rutgers!modus!simm!yachaya!oliver!newsuser Newsgroups: comp.unix.programmer,comp.lang.c,sublink.comp.unix Keywords: shmat sysv Reply-To: cei@ipvvis.unipv.it (Ugo Cei) Followup-To: comp.unix.programmer Distribution: comp Organization: Oliver, Pavia, Italy Lines: 57