Xref: utzoo comp.unix.xenix:2583 comp.sources.d:2380 Path: utzoo!attcan!uunet!sco!rosso From: rosso@sco.COM (Ross Oliver) Newsgroups: comp.unix.xenix,comp.sources.d Subject: Re: tgetent core dump on sco xenix Summary: Aren't segments fun? Keywords: tgetent,core dump,sco,xenix,large model Message-ID: <701@nod2sco> Date: 2 Jul 88 06:48:21 GMT References: <54@libove.UUCP> Distribution: comp Organization: SCO Technical Support Lines: 34 In article <54@libove.UUCP> Jay Libove illistrates the single most common mistake in large-model 286 programs. The example he gives, when compiled using "cc -M2l..." will dump core on execution: >#include >#include > >main() > { > char buffer[1024]; > > printf("%d\n",tgetent(buffer,getenv("TERM"))); > }; Note that the return value of getenv() is not declared, therefore, the compiler assumes int. However, tgetent is expecting a char *, and in large-model 286 programs, int != char *. So, when main() calls tgetent, it passes only two of the four bytes returned by getenv(). tgetent picks up garbage data as the other two bytes, and (suprise!) a segmentation violation results. How can this be fixed? With the following declaration: char *getenv(); Here are a few tips to avoid problems like this in large-model 286 programs: - Declare your functions' return values. - Don't use "0" to mean NULL ( i.e. (char *)0 ). - Don't interchange ints and pointers, or longs and pointers. Ross Oliver SCO Technical Support