Subject: Re: wxwin: Does anyone understand why this c-code fails
From: [email-masked] ("David C. Hoos, Sr.")

26 Oct 1996 21:50:02 -0700
Sender: [email-masked]
Approved: [email-masked]
Distribution: cygnus
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Original-To: "Keith Gary Boyce" <[email-masked]>, <[email-masked]>
X-MSMail-Priority: Normal
X-Priority: 3
X-Mailer: Microsoft Internet Mail 4.70.1155
Original-Sender: [email-masked]

Hi Keith,

The code fails because you're attempting to alter memory which is
read-only.

This comes about from a faulty declaration for "name" in "main."  Your
declaration says "allocate space for a variable which is a pointer to char,
and initialize it with the address of a string literal which you supply. 
The memory for the string literal is in a read-only data segment allocated
by the compiler/linker.  It is done this way, because if you had used an
identical string literal in a similar context elsewhere in the same
compilation unit, the compiler could have used the same memory for both. 
You can see, that in such a circumstance, allowing the program to alter
such a string would be disastrous.

In other words, the only VARIABLE you have declared is the variable holding
the address of a string, and specified an initial value.

What you need to do instead is to declare "name" as follows:
char name [] = "C:\\tmp\\foo";
What this declaration says is allocate me space for a variable which is an
array of char, sufficiently large to hold the string literal with which I'm
initializing it, plus the terminating NUL.   Now the characters are
variable, which is what you want to do.

Personally, it's stuff like this which makes me prefer to develop software
in Ada, because an Ada compiler would never let you compile a program like
this which compiles with no warnings or errors, then promptly crashes on
execution.

Hope this helps 
David C. Hoos, Sr.
http://www.dbhwww.com
http://www.ada95.com

----------
> From: Keith Gary Boyce <[email-masked]>
> To: [email-masked]
> Subject: wxwin: Does anyone understand why this c-code fails
> Date: Saturday, October 26, 1996 3:26 PM
> 
> Does anyone know why the following code dumps when in
> wxUnix2DosFilename function. I'm a little rusty on pointer stuff
> #include <stdio.h>
> #include <string.h>
> #define WIN95
> #define wx_msw
> 
> void
> wxUnix2DosFilename (char *s)
> {
> // Yes, I really mean this to happen under DOS only! JACS
> #ifdef wx_msw
> 	int offset=0;
> #ifdef WIN95
> 	if (isalpha (*s) && *(s + 1) == ':')
>         	offset=2;
> #endif
> 
>   if (s+offset)
>     while (*(s+offset))
>       {
> #ifdef WIN95
> 	if (*(s+offset) == '\\')
> 		*s = '/'; 
> 	else
> 		*s = *(s+offset);	
> #else		
> 	if (*s == '/')
> 	  *s = '\\';
> #endif
> 	s++;
>       }
> #ifdef WIN95
> 	*s=*(s+offset);
> 	printf("%s\n",s);
> #endif
> #endif
> }
> 
> void main()
> {
> char c;
> char *name = "C:\\tmp\\foo";
> FILE* f;
> 
> wxUnix2DosFilename (name);
> //f = fopen(name, "r");
> //while ((c = getc(f)) != EOF) {
> //printf("%c", c);
> //} /* while */
> //fclose(f);
> }
> 
> -------------------------------------
> Name: Garry Boyce
> E-mail: [email-masked] (Garry Boyce)
> Date: 12/01/94
> Time: 08:46:10
> 
> This message was sent by Chameleon 
> -------------------------------------
> 
> -
> For help on using this list, send a message to
> "[email-masked]" with one line of text: "help".
-
For help on using this list, send a message to
"[email-masked]" with one line of text: "help".