delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/03/06/22:29:43

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-Id: <5.1.0.14.2.20020306192102.02396df0@pop3.cris.com>
X-Sender: rrschulz AT pop3 DOT cris DOT com
X-Mailer: QUALCOMM Windows Eudora Version 5.1
Date: Wed, 06 Mar 2002 19:29:36 -0800
To: hongming AT anet DOT ne DOT jp, cygwin AT cygwin DOT com
From: Randall R Schulz <rrschulz AT cris DOT com>
Subject: Re: stackdump about C language
In-Reply-To: <20020307120108.9A9E.TAOISM@mx9.freecom.ne.jp>
Mime-Version: 1.0

Hello,

[ No Cygwin-specific issues here. ]


The compiler and / or linker are allowed to place those string literals in 
read-only storage, and apparently gcc under Cygwin does just that.


If you modify your program like this:

-==-
#include <string.h>

char *strsave(char *);


int main() {
//  char *a = "I am a teacher";
//  char *b = "You are a student";
     char *a = strsave("I am a teacher");
     char *b = strsave("You are a student");
     printf("string_a = %s\nstring_b = %s\n", a, b);
     copy_string(a, b);
     printf("string_a = %s\nstring_b = %s\n", a, b);
}

int
copy_string(char *from, char *to) {
     while((*to++ = *from++) != '\0');
}


char *
strsave(char *str)
{
     char *newStr = (char *) malloc(strlen(str) + 1);
     strcpy(newStr, str);
     return newStr;
}
-==-


It will run without faulting and do what (I believe) you expect. Note you 
are relying on the fact that "I am a teacher" is shorter than "You are a 
student"

Randall Schulz
Mountain View, CA USA



At 19:09 2002-03-06, taoism AT mx9 DOT freecom DOT ne DOT jp wrote:
>Hi, gentleman, could you do me a favour?
>I had some trouble in running a C program.
>
>[C source code is]
>-----  from  here ----
>#include <stdio.h>
>
>int main() {
>   char *a = "I am a teacher";
>   char *b = "You are a student";
>   printf("string_a = %s\nstring_b = %s\n", a, b);
>   copy_string(a, b);
>   printf("string_a = %s\nstring_b = %s\n", a, b);
>}
>
>int copy_string(char *from, char *to) {
>   while((*to++ = *from++) != '\0');
>}
>-----  end  here ----
>
>[Compilation Tool]
>   gcc version 2.95.3-5(cygwin)
>
>[Question]
>  The compilation is passed, but after running the a.exe, the
>following message appeared and I got a a.exe.stackdump too.
>------
>string_a = I am a teacher
>string_b = You are a student
>      0 [main] a 1536 open stackdumpfile:Dumping stack trace to a.exe
>stackdump
>Segmentation fault (core dumped)


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


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