delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/01/18:47:01

From: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: general protection fault
Date: 1 Dec 1997 07:01:02 GMT
Organization: The National Capital FreeNet
Lines: 32
Message-ID: <65tnbe$321@freenet-news.carleton.ca>
References: <Pine DOT SOL DOT 3 DOT 96 DOT 971130120351 DOT 20882A-100000 AT welchlink DOT welch DOT jhu DOT edu>
Reply-To: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
NNTP-Posting-Host: freenet5.carleton.ca
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019