This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: stackdump about C language


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@mx9.freecom.ne.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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]