Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!husc6!think!ames!amdcad!sun!gorodish!guy From: guy%gorodish@Sun.COM (Guy Harris) Newsgroups: comp.lang.c Subject: Re: Allocation for pointers Message-ID: <27282@sun.uucp> Date: Fri, 4-Sep-87 16:18:04 EDT Article-I.D.: sun.27282 Posted: Fri Sep 4 16:18:04 1987 Date-Received: Sat, 5-Sep-87 22:42:42 EDT References: <1357@faline.bellcore.com> Sender: news@sun.uucp Lines: 43 > main() > { char *a; > a = "text"; > recurs(a); > } > > void recurs(a); > char *a; > { strcat(a,"more text"); > if (some conditional test) recurs(a); > } > > As I understand it, I'm passing the address of the variable a into > the routine recurs. No, you are passing the *value* of the variable "a" into the routine "recurs"; it points to a 5-byte string. > At no point do I specify how large of a buffer a will eventually point to. No, the value of "a" is not changed after it has the address of the string "text" assigned to it, so it always points to a 5-byte string. "recurs" just passes it along to itself. In ANSI C, at least, there is no guarantee that this is a 5-byte buffer; the compiler can e.g. place it into a non-writable portion of your address space, so this code may just drop core. In *no* implementation that I know of is there any guarantee that you can append anything such as the "more text" to the end of it; I suspect that in most implementations that allow you to write something after the string at all, you will be scribbling on some random portion of your address space, possibly on top of some other strings. > Is there a limit and is it compiler dependent? Yes, this will blow up very quickly on most compilers, since the "strcat" will at best be writing on a random portion of your data space and will at worst attempt to write into a non-writable portion of your data space. If you want dynamically-extendable strings, you'll have to implement them yourself. Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com