ferneau@silver.UUCP (10/02/87)
/* Written 4:19 am Oct 2, 1987 by ferneau@silver.UUCP in silver:cs.unix.bacs */
/* ---------- "Ultrix-2.0 calloc bug?" ---------- */
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.
Here is the program I am using:
/*
* tmp.c
*
* copy a given argument list into dynamic memory
*
*/
#include <stdio.h>
#include <memory.h>
#define ERR stderr
char **replicate();
char *Me;
main(argC,argV)
int argC;
char **argV;
{
int argc;
char **argv;
int x;
Me = *argV;
argc = argC;
argv = replicate(argC,argV);
for (x=0; x<argc; x++)
fprintf(ERR,"av[%u] = %s\n", x, argv[x]);
}
char **
replicate(ac,av)
int ac;
char **av;
{
char **nav;
int x;
if ((nav = (char **) calloc(ac,sizeof(char *)))==NULL) {
fprintf(ERR,"%s: out of memory\n", Me);
exit(1);
}
for (x=0; x<ac; x++) {
if ((nav[x]=(char *)calloc(sizeof(av[x])+1,1))==NULL) {
fprintf(ERR,"%s: out of memory\n", Me);
exit(1);
}
if (x)
fprintf(ERR,"av[%u] = %s\n", x-1, nav[x-1]);
fprintf(ERR,"av[%u] = %s\n", x, nav[x]);
strcpy(nav[x],av[x]);
fprintf(ERR,"av[%u] = %s\n", x, nav[x]);
}
return(nav);
}
----------------------------------------------------------
End of Program
----------------------------------------------------------
I type "tmp 123456789012345678901234567890 argument" to the shell, and
this is what I get back:
av[0] =
av[0] = tmp
av[0] = tmp
av[1] =
av[1] = 123456789012345678901234567890
av[1] = 12345678901256
av[2] =
av[2] = argument
av[0] = tmp
av[1] = 12345678901256argument
av[2] = argument
You will notice that av[1] is corrupted only after a call to calloc (see
the code in _replicate()_. I assume that this is a problem in the
c-library. I would appreciate any help anyone could give.
ferneau@silver.bacs.indiana.edu
.
/* End of text from silver:cs.unix.bacs */chris@mimsy.UUCP (10/03/87)
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<ac; 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
finegan@uccba.UUCP (Mike Finegan) (10/05/87)
While I haven't tried your code on our Ultrix 2.0 (11/750), I have
had problems before with passing pointers (to system used memory)
(i.e. argV) after various operations (I can go look ...). I believe
one of the operations was passing "argv" to a function. This may
be different, but what guarantee do you have about system allocated,
and used memory; how long is it 'static', etc.? Sorry if this sounds
vague, but I avoided the problem by doing it another way (copying and not
accessing again).
Mike Finegan
...{pyramid,decuac,mit-eddie,...}!uccba!finegan