Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP Newsgroups: comp.unix.wizards Subject: Re: Ultrix-2.0 calloc bug? Message-ID: <8899@mimsy.UUCP> Date: Sat, 3-Oct-87 13:02:58 EDT Article-I.D.: mimsy.8899 Posted: Sat Oct 3 13:02:58 1987 Date-Received: Sun, 4-Oct-87 06:54:39 EDT References: <12300001@silver> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 45 In article <12300001@silver> ferneau@silver.bacs.indiana.edu writes: >I am having a problem with dynamic memory allocation on silver (a >VAX 8650 running Ultrix 2.0) using the calloc() routine, which seems >to be corrupting my array. The bug is in your program, not in calloc() (which is not to say that there cannot be a bug in calloc() too). Everything up to the following looks fine after a quick scan; then: >char ** >replicate(ac,av) >int ac; >char **av; >{ > char **nav; > int x; > > if ((nav = (char **) calloc(ac,sizeof(char *)))==NULL) { It is good that you test the return value; you also, however, need to declare `calloc' as `char *calloc()', or, in dpANS C: char *calloc(int nitems, unsigned int itemsize); (note that I prefer to name the arguments so that someone scanning the declaration can see what the seventh `int' is for.) Moving along: > for (x=0; x if ((nav[x]=(char *)calloc(sizeof(av[x])+1,1))==NULL) { Here is the bug. `sizeof av[x]' is the size of `char *', not the length of the string stored in that particular `char *'. Make this if ((nav[x] = calloc(strlen(av[x])+1, sizeof(char))) == NULL) { (the cast no longer being needed, and `sizeof(char)' being a hedge against `char' perhaps being multi-byte, and as a bit of documentation) and the program should work. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris