HDTenEightyP/NetNews huggingface dataset:
From: msb@sq.sq.com (Mark Brader)
Subject: Re: definition of strncpy stupid?
Summary: No, don't use sprintf() after all.
Organization: SoftQuad Inc., Toronto, Canada
Date: Wed, 23 Dec 92 10:00:54 GMT

> I realise this thread has been running for a while, but I think I have
> something new to say.

Me too!

I too advocated the use of sprintf() with a "%.*s" format directive
as a replacement for strncpy() which writes exactly one null.  And I
still stand by this recommendation for most purposes, but there is
an important caveat which I forgot, and have not seen mentioned here.

Which is: an implementation is allowed to limit the length of the
string converted by a single format directive in any of the printf()
family functions.  In ANSI C, the limit may be as short as 509 bytes.
Now in most cases when you use strncpy(), you know what the length
argument is, and it *is* a number smaller than 509.  But if this is
not the case, then do *not* use sprintf() to replace strncpy().

Unfortunately, if you do have to deal with very long strings (in which
case efficiency is more likely to matter), there isn't really any way
of doing a length-limited copy that's known to be efficient and to work.

If you say
	strncpy (dest, src, n-1);
	dest[n-1] = '\0';
then strncpy() has to write a lot of nulls if the string is short.

If you say
	memcpy (dest, src, n-1);
	dest[n-1] = '\0';
then you risk a memory access error if the source string comes from
an array shorter than n-1 bytes, and you're copying unnecessary bytes
if the string is short.

If you say
	if (strlen (src) >= n) {
		memcpy (dest, src, n-1);
		dest[n-1] = '\0';
	} else
		strcpy (dest, src);
then you are taking an extra pass through the source string, which is
inefficient if it is long.

If you say
	int tmp;
	tmp = src[n-1];
	src[n-1] = '\0';
	strcpy (dest, src);
	src[n-1] = tmp;
then you risk a memory access error if the source string is not writable,
or if it comes from an array shorter than n bytes.

If you say
	sprintf (dest, "%.*s", n, src);
then you run into the limitation noted above.

And if you say
	int i;
	for (i = 0; i < n && src[i]; ++i)
		dest[i] = src[i];
	if (i == n)
		dest[--i] = '\0';
, or the equivalent with advancing pointers, then you risk the code
being significantly slower than an efficient library implementation of
one of the other methods.

Sigh.

NOTE: none of the code fragments in this article have been tested.
Use at your own risk.
-- 
Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com
	"I'm a little worried about the bug-eater," she said.  "We're embedded
	in bugs, have you noticed?"		-- Niven, "The Integral Trees"

This article is in the public domain.
From: scjones@thor.sdrc.com (Larry Jones)
Newsgroups: comp.lang.c
Subject: Re: speed?  double vs. float.  math.h??
Date: 1 Jan 93 20:50:47 GMT

In article <PRmewB2w165w@quest.UUCP>, kdq@quest.UUCP (Kevin D. Quitt) writes:
>     It depends on your machine.  If your machine uses IEEE format
> floating point, and has hardware to do the calculations, then the
> internal calculation format is probably always double, if not long
> double.  (Motorola's FPUs use the 96bit long double format.  Intel's
> FPUs use double format).

Not quite -- Intel FPUs normally use an 80 bit long double format
although some operations are affected by a settable precision (float,
double, or long double).  Current FPUs default the settable precision
to long double and Intel recommends leaving it that way.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
larry.jones@sdrc.com  or  ...uunet!sdrc!larry.jones
I kind of resent the manufacturer's implicit assumption
that this would amuse me. -- Calvin
Xref: sparky comp.lang.c:18828 comp.unix.bsd:10587
From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
Newsgroups: comp.lang.c,comp.unix.bsd
Subject: Re: Segmentation faults
Date: 23 Dec 92 17:03:37 GMT
Organization: McGill Research Centre for Intelligent Machines

In article <veit.724925472@du9ds3>, veit@du9ds3.fb9dv.uni-duisburg.de (Holger Veit) writes:
> In <1992Dec18.134225.20548@Logix.DE> jpm@Logix.DE (Jan-Piet Mens) writes:
>> In <1gricmINNsl@ub.d.umn.edu> cbusch@ub.d.umn.edu (Chris) writes:
>>>    Why is the following code producing a segmentation fault???
>> void writexy(x,y,s)
>> int x, y;
>> char *s;				/* OTHERWISE DEFAULTS TO int !!! */

Right you are.  This was the only thing I saw that was obviously
blatantly wrong, so I would conjecture that it's what's at fault.

> This is a real problem where sizeof(int) != sizeof(char*), for
> instance with Turbo C or Microsoft C or similar 16bit junk.  But if
> you compile this with a 32bit K&R compiler, and
> sizeof(int)==sizeof(char*) (=4), it won't see a difference, because
> it will just take 32bit from the parameter stack and push it as a
> 32bit argument to printf.

You're assuming arguments go on a stack, with ints and char *s passed
the same way.  A compiler for a 68000-family machine may well pass x
and y in d0 and d1 with s in a0.  Of course, when s is undeclared (and
thus defaulting to int) it would go in d2.  Thus, when writexy fetches
s, it will use d2 rather than a0, and get junk.  (Printf will also have
problems, which I'm sure you can fill in from this sketch.  <varargs.h>
would be a little more complicated under such a system, but by no means
impossible.)

> The compiler cannot find out the real expected type (it cannot do
> parsing of the format string in general!).

Actually, under ANSI, it can, when the format string's contents can be
determined at compile time, as they can here.  Not that it helps,
because it is compelled to treat s as int.  (It would be free to
produce a warning, of course; compilers are always free to produce
whatever warnings they please.)

					der Mouse

			    mouse@larry.mcrcim.mcgill.edu
Newsgroups: comp.lang.c
From: alan@sal.wisc.edu (Alan Watson)
Subject: Re: a question about if and case
Organization: Space Astronomy Lab, Madison WI
Date: Thu, 24 Dec 1992 18:11:29 GMT

In article <1992Dec24.092119.21197@jonlab.UUCP> jon@jonlab.UUCP (Jon LaBadie) writes:
>In article <1gi6poINN3lv@nz12.rz.uni-karlsruhe.de> "hans friedrich steffani" <GM08@DKAUNI2.BITNET> writes:
>>[Query about efficient "if-elseif-" vs switch code]
>
>[Discussion of the "optimal" construction based on 1986
> compiler technology on an unspecified architecture.]

You should write clear, straightforward code and rely on your compiler
to optimize it for you.  If your compiler cannot transform between
switches and series of if-elses, then buy a better compiler.

>Change only when performance has become an issue and you note,
>through profiling, that the multi-way branch is a problem.

Good advice.  Unless you are writing a byte-code interpretter, the 
additional overhead of a less than optimal construction is probably
negligible.
Newsgroups: comp.lang.c
From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
Subject: Re: sccs eats good code -
Organization: Techn. University of Berlin, Germany
Date: Tue, 22 Dec 1992 03:54:08 GMT

> Distribution: comp
Whoever added this should get their software fixed.

kaleb@jpl-devvax.jpl.nasa.gov (Kaleb Keithley) writes:
>In article <1992Dec21.4396.29992@dosgate> peter.curran@canrem.com writes:
>> A simple solution is to replace the original string with something
>> like "%w%\H%\M%\S".  Ugly, but it works. [..]
>
> I think this bites everyone who uses SCCS.  I agree that Peter's solution is
> ugly, because the person who' maintaining the code comes along, doesn't 
> immediately recognize why the backslashes are there, and strips them out 
> without thinking.

And rightly so, not only because you apparently think that support
people can neither think nor read (comments).

In ANSI C, backslashes in front of anything except x[0-9A-Fa-f]+|
[0-7]{1,3}|[abfnrtv'"\?] yield undefined behaviour.  (Vendors may add
their own escape sequences to the set, or may have already done so,
without becoming nonstandard.)

If you're restricted to ANSI C, stick to string literal concatenation.
I can't think of an elegant solution that is both portable between
ANSI and K&R C and between different character encodings.

Another portable alternative to the suggested

	static char fmt[] = { '%','w','%','H','%','M','%','S','\0' };

(braces courtesy of International Brace Fund for the Needy ;-) would be

	if (strftime(time_str, 64, "%w%H\
%M%S", time_struct) != 7)

which is an even uglier kludge, but easier to automate and a more
local change.

Jutta Degener (jutta@cs.tu-berlin.de)
Newsgroups: comp.lang.c,comp.lang.c++,comp.misc
From: mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
Subject: Re: What is Object Oriented Programming? Is C doomed?
Organization: Texas Instruments Inc
Date: Thu, 31 Dec 1992 14:34:56 GMT

In <1992Dec30.204142.16778@informix.com> cshaver@informix.com (Craig Shaver) writes:

>In article <1992Dec30.173627.2696@netcom.com> nagle@netcom.com (John Nagle) writes:
>>swillden@news.ccutah.edu (Shawn Willden) writes:
>>>	Is this scheme similar to the one you had in mind?  If yours
>>>is better, please share it (or provide references, I suppose I ought
>>>to check Knuth to see if he has anything like this).
>>>In any case, I like the idea well enough that I think I'm going to
>>>start using it.
>>
>>       Look into B-trees for this sort of thing.  But don't look in
>>Knuth's "Art of Computer Programming" for them; they postdate that classic
>>work.
>>
>>					John Nagle

>I believe Knuth covers b-trees and b+trees in vol. 3, sorting and searching.

Quite correct.  See _The Art of Computer Programming_, Vol 3, "Sorting
and Searching", on pp 473-480.  B-trees and refinements of B-trees are
covered as one part of the chapter on multiway trees under "Sorting".

-- 
"Insisting on perfect safety is for people who don't have the balls to live
 in the real world."   -- Mary Shafer, NASA Ames Dryden
------------------------------------------------------------------------------
Fred.McCall@dseg.ti.com - I don't speak for others and they don't speak for me.
Newsgroups: comp.lang.c
From: msb@sq.sq.com (Mark Brader)
Subject: Re: A different C puzzle
Summary: impossible
Organization: SoftQuad Inc., Toronto, Canada
Date: Wed, 23 Dec 92 09:27:12 GMT

> > Write a [C] program that uses every valid character exactly once.
> I should think it would be rather difficult to use only one " ...

You could manage that with a #define or a character constant, but each
of those also involves a repeated character (e or ') and so is ruled
out.  I think it's clear that the problem has no solution that is a
strictly conforming ANSI C program.  You must have a "main()", and that
already means that (a) you can't call any other functions (parentheses);
and (b) you can't have a return statement (two r's).  So you can't
terminate in a permissible way.  I note in addition that every
preprocessor directive either has a letter in common with "main"
or has a repeated letter; thus no preprocessor tricks could be used.
-- 
Mark Brader		    "Howeb45 9 qad no5 und8ly diturvrd v7 7jis dince
SoftQuad Inc., Toronto	     9 qas 8mtillihemt mot ikkfavpur4d 5esoyrdeful
utzoo!sq!msb, msb@sq.com     abd fill if condif3nce on myd3lf."      -- Cica

This article is in the public domain.
Newsgroups: comp.lang.c
From: jsd@nynexst.com (Joseph Delotto)
Subject: allocating 2-d array
Organization: NYNEX Science & Technology, Inc.
Date: Tue, 22 Dec 92 18:55:23 GMT

Sorry if this is too simple, but I can't find a good example in
any books.  I want to dynamically allocate a 2-d array for a
variable defined as:

int	**myarray;

where the dimensions = nrows,ncols.  Currently I'm using the
following code:

myarray = (int **) malloc ((unsigned) nrows*sizeof(int*));
for(i=0;i<=nrows;i++)
     myarray[i] = (int *) malloc ((unsigned) ncols*sizeof(int));

which seems to work ok but looks awfully sloppy.  Is there any
way to get the whole thing done in one shot?

Thanks for any help,

Joe DeLotto
jsd@nynexst.com
Newsgroups: comp.lang.c
From: asj4j@holmes.acc.Virginia.EDU (JonaX)
Subject: BORLAND C++ 3.0 FOR SALE
Organization: University of Virginia
Date: Sun, 3 Jan 1993 21:29:29 GMT

Hello,

   I have the BORLAND C++ version 3.0 for sale.  It includes the Turbo
Debugger 3.0, Turbo profiler 2.0, Turbo Assembler 3.0, and the following
manuals that are in mint condition:

        1) Borland C++ 3.0 User's Guide
	   -Integrated Environment
	   -Optimization
	   -Command-Line Compiler
	   -Installation

	2) Borland C++ 3.0; Programmer's Guide:
           -Language Structure
           -Class Libraries
           -Advanced Programming Techniques
	   -Ansi C Implementation

	3) Borlnad C++ 3.0 Library Reference
	   -Runtime Library
	   -Global Variables
	   -Cross-Reference

	4) Borland 3.0 Tools & Utilities Guiede
	   -Error Messages
	   -Winsight
	   -Make
	   -Help/Resource Compilers
	   -Tlink

	 5) Resource Workshop - User's Guide

	 6) Turbo Assembler 3.0 - Quick Reference Guide

	 7) Turbo Debugger 3.0 - User's Guide (Still Wrapped!)

	 8) Turbo Profiler 2.0 - User's Guide (Still Wrapped!)

	 9) Turbo Assembler 3.0 - User's Guide (Still Wrapped!)

   These manuals have barely been used and are in new-mint condition.
The last 3 manuals have never been unwrapped and therefore never even been
used!  Also comes with a Borland C++ Quick Reference Booklet.

   Comes on nine 5(1/2) inch HD diskettes.  Also one Borland Sample Icons 
diskette included.  Also includes a 5(1/2)" low-density or 3(1/2)" Low
Density DISK ORDER COUPON, just in case you your computer doesn't have a
5(1/2)" HD drives.  You have to send in this original coupon with a 
photocopy of the registration card to get add these additional diskettes.

   Also includes the original REGISTRATION CARD for you to fill out and 
send in to Borland.    

   This package originally cost me over $400, I'm looking for $200..OBO...
First come first serve policy, no bidding.

						Art Jonak

						asj4j@virginia.edu
Newsgroups: comp.lang.c
From: dirsolns@world.std.com (Bernard Farrell)
Subject: Re: Arrays with variable dimensions?
Organization: Directed Solutions Group
X-Newsreader: Tin 1.1 PL3
Date: Wed, 30 Dec 1992 13:10:05 GMT

ah017@yfn.ysu.edu (John B. Lee) writes:
: Is it possible to create an array that would change size according
: to user input?
: 
: John Lee
: usr7245a@cbos.uc.edu
: ah017@yfn.ysu.edu
: -- 

You can do this using the following kind of approach:

#define   ARRAY_INCR	(20)

float fl_arr[] = NULL;   /*  This is actually a float * -- no space  */
int   fl_max = 0;        /*  Max. num of values allowed in array.  */
int   fl_ind = 0;        /*  Next slot to use in array.   */
float new_val;           /*  Holds next inserted element  */
....
  /*  Get the next element and insert it into the 'array'  */
  new_val = function_to_get_float_value();

  if (fl_ind >= fl_max)
  {
    /* Time to increase the array size */  
    fl_arr = (float *) realloc(fl_arr,
			       sizeof(fl_arr[0])*(fl_max+ARRAY_INCR) );
    if (NULL == fl_arr)
    {
       /* Out of memory -- we're in trouble  */
       print_horrible_error_and_exit();
    }

    /*  Increment the maximum indicator  */
    fl_max += ARRAY_INCR;

  }  /*  If we're out of room in array  */

  /*  Now insert the new value  */
  fl_arr[fl_ind++] = new_val;

--------------------------------------

This allows the array to increase in size as required.  The
alternative is to do a malloc(), instead of using realloc(),
based on a value given at run-time.   Note that the above
code works only when realloc() works as malloc() when the
first parameter is NULL -- some implementations of realloc()
do not have this behavior.

Hope this helps.


-- 
|----------------------------------------------------------|
|      Bernard Farrell      |   Directed Solutions Group   |
|----------------------------------------------------------|
|   dirsolns@world.std.com  |       (617) 429-2306         |
Newsgroups: comp.lang.c,comp.os.msdos.programmer
From: ST21519@VM.CC.LATECH.EDU
Subject: Re: Power C (MIX C) Compiler QUESTION
Organization: Louisiana Tech University
Date: Tue, 29 Dec 92 21:25:53 CST

 > I'm trying to use Power C to generage object files to link with Turbo
 > Pascal.  To do this, I'm using the /q compiler directive to make
 > Microsoft compatible object files.  But the option seems to have no
 > effect.  I can't link the resulting object file with the Pascal program.
 > Has anyone used the /q option in Power C successfully??
 
Per a voice conversation I had with MIX tech support before purchasing PowerC
last week, you cannot link any object code with MIX execept for MASM.
 
Consult your MIX manual on page 615...
 
The MIX utility translates a Microsoft object (.obj) into a MIX object (.mix).
 
I do not know what would be different between an .obj created with MASM or
QB...was going to pose that question here or in another forum.  I have not
attempted to translate an .obj I have (QB, PDS) so I do not know from first
hand experience - just from tech support and the manual.
 
Newsgroups: comp.lang.c
From: bandy@netnews.jhuapl.edu (Mike Bandy)
Subject: Re: upper/lower case with VMS C
Organization: JHU/Applied Physics Laboratory
Date: Mon, 28 Dec 1992 14:48:12 GMT

david@rex.uokhsc.edu (David W. Bourne) writes:

>I am trying to compile a 'c' program I obtained from a colleague under
>VAX VMS. With other module I had no real problems but one module
>apparently has variables with the 'same' names, that is only case
>different.

>One line reads

>  double *Ao, *Ak, .... , *ao

>I then get an error message CONFLICTDECL indicating that the ao is
>a redeclaration of the Ao variable.
 ...
>I have been able to compile the module on a UNIX machine without a problem.
>It seems to be the VAX VMC 'c' environment that is causing a problem.

Define the variables as 'static' so that the compiler keeps them local to
the current source module.  Then there won't be a conflict with other,
globally defined, variables of similar names.  Are all definitions
of the same type?  Maybe the error message is telling you that you have
float/double conflict that the Unix system resolves in a different manner
than VMS.
-- 

	Mike Bandy
	bandy@aplcomm.jhuapl.edu
	Johns Hopkins University / Applied Physics Lab
Newsgroups: comp.lang.c
From: mikes@Ingres.COM (Mike Schilling)
Subject: Re: Program to convert .EXE to a "C" source file?  I really need one!!
Date: 4 Jan 93 02:22:41 GMT

From article <1993Jan2.203527.27050@news2.cis.umn.edu>, by dutc0006@student.tc.umn.edu ():
> In article <1993Jan1.225604.964@blurt.oswego.edu> ch_ind05@blurt.oswego.edu (Greg Fuller (SUNY Oswego, Oswego New York)) writes:
>>Hello. 
>>	I am looking for a program that will take a .EXE or .COM file and
>>convert it into a "C" source file.  I saw one on one of the FTP sites today,
> [Stuff deleted]
> 	I am looking for a program of that nature too.  If someone knows 
> where one can be FTP'd I would appreciate E-Mail telling me.  
> 				Thanks,
> 				dutc0006@student.tc.umn.edu			
> 
I'd prefer a program that generates the RCS or SCCS archives the program was 
developed in, plus a makefile.  Architectural and design documents would be 
nice too.
----------------------------------------------------------------------------
mikes@ingres.com = Mike Schilling, INGRES, An ASK Group Company, Alameda, CA
Just machines that make big decisions,
Programmed by fellows with compassion and vision.	-- Donald Fagen, "IGY"
From: tpradeep@cs.tamu.edu (Pradeep K Tapadiya)
Newsgroups: comp.lang.c
Subject: Is there any utility to construct `C' program flow.
Date: 4 Jan 1993 00:03:36 GMT
Organization: Computer Science Department, Texas A&M University
Distribution: usa
NNTP-Posting-Host: neuron.tamu.edu

Greetings,

I am wondering if there is any utility which will construct the
program flow from `C' source modules - something like

   main - Init     - Fnc1
                   - Fnc2
        - DoRead   - ReadParams
                   - ReadFile   - File1
                                - File2
        -

Can someone please tell me if there is any such shareware or public
domain utility?

Thank you for your help.

Pradeep
tpradeep@cs.tamu.edu

Newsgroups: comp.lang.c
From: dfox@quack.sac.ca.us (David Fox)
Subject: Unix equivalent to DOS kbhit() function?
Keywords: kbhit, getch, low-level keyboard control
Organization: The Duck Pond public unix: +1 408 249 9630, log in as 'guest'.
Date: 27 Dec 1992 22:09:08 UTC

puts("hello");

I am a relatively new Unix user, although I've used DOS C (MSC/Turbo C)
for sometime.  I've read the FAQ about how to get only one character
from the console, which pertains to using getch() in curses, which 
seems to work all right, but that's not really what I'm after.  What I
want is to do something similar in Unix to what the DOS C kbhit() call
does - which only returns something if the keyboard has been hit.  The
idea here is to run something in a loop until a keypress.


Newsgroups: comp.lang.c
From: gordon@sneaky.lonestar.org (Gordon Burditt)
Subject: Re: speed?  double vs. float.  math.h??
Organization: Gordon Burditt
Date: Sun, 27 Dec 1992 18:38:31 GMT

>     I'm currently writing a C program where it needs to do lots and lots of
>trig calculations.  But I would like the program to run really really fast.
>So I'm concerned about the speed at which the calculations are carried out.

Be careful, you might be shooting yourself in the foot.  *MEASURE*
the differences.  

>Trying to make the program run fast, I would like to have only float 
>variables, instead of doubles, in my program.  But in the standard math 
>library, all the functions take in arguments and return values in the type 
>double.  So, what should I do?  First of all, should I even be concerned 

Taking, for example, the 486DX, mostly because I have the book
handy, the only instructions that are affected by the precision
control bits are add, subtract, multiply, divide, and square root
(not the trig functions, etc.).  Only the divide instruction mentions 
timing being different based on precision control.  Storing a real 
number is 1 clock different in 32 bits vs. 64 bits, but storing an 
80-bit real is one clock FASTER than storing a 32-bit real!  

If the compiler is generating actual code to do conversions between
float and double, a few clocks saved on stores may be overwhelmed
by the conversion code.

						Gordon L. Burditt
						sneaky.lonestar.org!gordon
Newsgroups: comp.lang.c
From: bdickens@elara.mitre.org (Brian Dickens)
Subject: UNIX pipes in C
Keywords: UNIX, pipes, C
Organization: Research Computer Facility, MITRE Corporation, Bedford, MA
Date: Tue, 29 Dec 1992 18:32:48 GMT

I am attempting to write a program that will control another program -- by
means of pipes.  For example, suppose I have one program that does math
calculations, called calc.  A sample interaction with calc might look like this:

% calc
CALC PROGRAM... Please enter your problem: 3+5
8
CALC PROGRAM... Please enter your problem: 8*8
64
CALC PROGRAM... Please enter your problem: quit
Thank you for using CALC PROGRAM.
%

Another program wishes to perform calculations with calc.  For example,
this program could be called frontend.  Frontend would look something
like this:

% frontend
FRONT END TO THE CALCULATOR... Please enter your expresssion: 2+7
RESPONSE FROM THE CALCULATOR is 9.  Thank you for using frontend!
%

The program which corresponds to the "calc" program above has already
been written.  I need to know what kind of code I have to write to set up a
link between my program (analogous to "frontend") and the "calc" program.
The manpages for fork() and pipe() are very confusing for me, and I don't
see how to do this.

Any help would be appreciated... Thanks!
<<<?>>> bdickens@elara.mitre.org <<<!>>>

Newsgroups: comp.lang.c