Article View: csu.cs
Article #66Re: I hate c
From: Michael Dwyer
Date: Sat, 22 Apr 2000 00:00
Date: Sat, 22 Apr 2000 00:00
47 lines
2504 bytes
2504 bytes
In csu.cs Adam Carheden <carheden@holly.colostate.edu> wrote:
: I have a c question, which, like most c questions, probably lies in the
: misunderstanding of pointers:
: I declare the following:
: udp_service = getservbyname("cs457", "udp");
: tcp_service = getservbyname("cs457", "tcp");
: UDP Protocol = tcp
: TCP Protocol = tcp
: Both structures always point to the same thing. Why?
Well, I have two theories:
1) C hates you. Yes, it is entirely possibly that C just detests
your very existence. It is rare, but I hear it happens. In
this case, it would suck to be you...
2) You just THINK that the two structures are different! Although
I don't see this in the man page, I have a suspicion that getserv
works in the same was as gethost. That is, it returns a pointer
to a static memory location that always holds the return values
of those calls.
Quick test? Try this:
printf("udp_service = %p\ntcp_service = %p\n",upd_service,tcp_service);
I suspect that the two pointers will be the same.
So, if this is the case, the second call overwrites the data from
the first call. They way to get around this is, if I recall
correctly, to use some bcopy() routines to copy off the answers
into some other location that is safe.
The funny this is that that huge book I gleefully ganked at the CSU LUG
meeting probably has some excellent sample code in it, just for this...
but I left the book at home. Oops. (looks under desk) Uh..l My other
book is at home, too.
Well... Find some example code somewhere. Its gotta be out there
somewhere...
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Michael J. Dwyer Digital Watches are a neat idea!
mdwyer@holly.ColoState.edu http://WWW.CS.ColoState.EDU/~dwyer/
Computer Science Major Amateur Radio Tech N0ZAP
Technical Details
Message-ID:
<39027596@news.ColoState.EDU>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39024CF4.C4286D1D@holly.ColoState.EDU>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 148df6,cb61e576f2f3a144
X-Google-Attributes: gid148df6,public
X-Google-Thread: 11c4c0,cb61e576f2f3a144
X-Google-Attributes: gid11c4c0,public
From: Michael Dwyer <mdwyer@holly.ColoState.EDU>
Subject: Re: I hate c
Date: 2000/04/22
Message-ID: <39027596@news.ColoState.EDU>
X-Deja-AN: 614521659
References: <39024CF4.C4286D1D@holly.ColoState.EDU>
X-Trace: 22 Apr 2000 22:01:26 -0700, holly.acns.colostate.edu
User-Agent: tin/pre-1.4-19990517 ("Psychonaut") (UNIX) (AIX/4-3)
Newsgroups: csu.cs,csu.cs.c++