Problems with ndbm

Chris Torek chris at mimsy.UUCP
Tue Aug 8 04:02:59 AEST 1989


In article <34200003 at zaphod> doug at zaphod.prime.com writes:
>What happens is something like:
>
>	datum *x;
>	x = dbm_fetch(thedb, thekey);
>	x -> dptr[x -> dsize] = '\0';
>	printf("%s\n", x -> dptr);  // Something is printed
>
>	x = dbm_fetch(thedb, thekey);
>	x -> dptr[x -> dsize] = '\0';
>	printf("%s\n", x -> dptr);  // Null is printed

This code will not even compile without warnings, and is clearly not
the code you used, since dbm_fetch returns a `datum' and not a `datum *'.
At a guess, I would say the original code is rather more like this:

	datum getkey() {
		datum key;
		char buf[SOMESIZE];
		key.dptr = buf;
		key.dsize = nnn;
		return (key);
	}

	...
		thekey = getkey();
		x = dbm_fetch(thedb, thekey);
		x.dptr[x.dsize] = '\0';
		printf("%s\n", x.dptr);

		x = dbm_fetch(thedb, thekey);
		x.dptr[x.dsize] = '\0';
		printf("%s\n", x.dptr);

in which it is just chance that the *first* call worked; or else,
the original code is something like this:

		thekey = dbm_fetch(thedb, otherkey);
		<same as before>
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.questions mailing list