Unix problem or C problem?

C Hudson Hendren III hudson at vsedev.VSE.COM
Fri Jan 20 03:08:12 AEST 1989


In article <245 at ibd.BRL.MIL> heilpern at brl.arpa (Mark A. Heilpern (IBD) <heilpern>) writes:
>
>	while (fscanf(input,"%d",stars)!=EOF) {
			         ^^^^^
You need to use an ampersand before the variable name so that fscanf has the
address of "stars".  The line should read:
	while (fscanf(input,"%d",&stars)!=EOF) {
>		fprintf(stderr,"OK to this point.\n");
>		fprintf(output,"%4d |",lines++);
>		stars /= 50;
>		for (count = 0; count <= stars; count++)
>			fprintf(output,"*");
>		fprintf(output,"\n");
>	}
>
>	I get the following error message, NOT put out from my program:
>
>PROTECTION VIOLATION: name='plot', pid=15943, pc=a410cdc, ps=80000180
>Illegal instruction
>
>	The 'plot' program is not setu/gid, I've never seen a PROTECTION
>	VIOLATION error like this before. Any insights would be greatly
>	appreciated.

It would appear that PROTECTION VIOLATION on you version of unix is what
most of us know to be a "segmentation violation".  This error occurs when
your program attempts to alter a memory location outside of it's allowed
area.

When you simply passed "stars" to fscanf() you were passing the actual value
of stars and not the address location of "stars".  The fscanf() function
expects an address.  If the value of "stars" happened to be zero, then
fscanf() would assume that you wanted the decimal number scanned in to be
stored at location zero.  This is either damaged another variable or it
clobbered an instruction (depending upon how your linker arranges the final
binary).  I guess that you clobbered an instruction since you got an illegal
instruction fault.
-- 
==> ..!uunet!vsedev!hudson  [hudson at vsedev.vse.com]  (C Hudson Hendren III) <==
==> These are my opinions and are not necessarily those of VSE Corporation. <==
==>	   MS-DOS was created to keep idiots away from UNIX computers	    <==



More information about the Comp.unix.wizards mailing list