Mail Archives: djgpp/1997/12/01/21:32:31
You're right--I guess my brain went on vacation there. I apologize for
the incorrect information.
On 1 Dec 1997, Paul Derbyshire wrote:
>
> Michael Phelps (loverns AT welchlink DOT welch DOT jhu DOT edu) writes:
>
> >> static char *string;
>
> ...
>
> >> string = "Hello Bobby";
> >
> > Looks like a problem here. Try allocating memory for it:
> > string = (char *)malloc(strlen("Hello Bobby")+1);
> > strcpy(string, "Hello, Bobby");
>
> What a waste of ram!
> String is declared a pointer to char, it will start off NULL.
> The assignment string="Hello Bobby"; does this: At compile time, "Hello
> Bobby\0" is embedded in the data segment of the object code, and into the
> executable at link time. The assignment becomes an instruction to get the
> address for that bit in the data segment (specifically wherever it loads
> into the address space at run time) and stuff it at the address for
> "string", meaning the pointer changes to point to the string in the data
> segment. Which is perfectly valid, allocated memory, and which has been
> nicely null-terminated for you at compile time. There's nothing wrong with
> that line of the original code and no reason to allocate yet more space at
> run time and put a duplicate of the string in there.
>
>
> --
> .*. Where feelings are concerned, answers are rarely simple [GeneDeWeese]
> -() < When I go to the theater, I always go straight to the "bag and mix"
> `*' bulk candy section...because variety is the spice of life... [me]
> Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh
>
- Raw text -