www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/18/23:48:08

Message-ID: <3510449E.AE294519@ipass.net>
From: Terry <iceman AT ipass DOT net>
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointers and DJGPP
References: <35103FD5 DOT A9F59A26 AT mail DOT ucsm DOT edu DOT pe>
Lines: 60
Date: Wed, 18 Mar 1998 22:02:19 GMT
NNTP-Posting-Host: customs59.ipass.net
NNTP-Posting-Date: Wed, 18 Mar 1998 17:02:19 EST
Organization: iPass.Net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

rpinnell AT characato DOT ucsm DOT edu DOT pe wrote:
> 
> Hi I have been trying to teach myself C for the last few months or so
> and have been using DJGPP.  I seem to be having a lot of problems
> whenever I use pointers with DJGPP.  Take the following example a simple
> strcat.
> 
> #include <stdio.h>
> 
> void mstrcat(char *s,char *t);
> 
> int main()
> {
> char ms1[]="Hello";
> char ms2[]=" World";
> mstrcat(ms1,ms2);
> printf("After mstrcat ms1 = %s\n",ms1);
> return 0;
> }
> 
> void mstrcat(char *s,char *t)
> {
>    while(*s++ != '\0')
>       ;
>    s--;
>    while((*s++ = *t++) != '\0')
>       ;
> }
> The program compiles fine and does what I want it to when it runs but
> exits with the following error message.
> Exiting due to signal SIGSEGV
> General Protection Fault at eip=000015dd
> eax=00000000 ebx=0004e100 ecx=00000000 edx=00000024 esi=00000054
> edi=0000c4c0
> ebp=6e6e6950 esp=0004c498 program=C:\WORK\A.EXE
> cs: sel=00af  base=8327b000  limit=0005ffff
> ds: sel=00b7  base=8327b000  limit=0005ffff
> es: sel=00b7  base=8327b000  limit=0005ffff
> fs: sel=0087  base=00009930  limit=0000ffff
> gs: sel=00c7  base=00000000  limit=ffffffff
> ss: sel=00b7  base=8327b000  limit=0005ffff
> 
> Call frame traceback EIPs:
>   0x000015dd
> Which is all Greek to me!  It isnīt just this simple program that I have
> problems with I seem to get similar error messages whenever I pass
> pointers to a function.  The same things work ok if I just use arrays
> rather than pointers.  Any advice would be aprreciated.  My apologies if
> this is the wrong place to ask this question  but it seems to me it is
> more a DGJPP/DOS question that a general C one.
> Richard Pinnell
> Arequipa Peru

Basically, you have defined ms1 to be only 5 characters long.  In
your subroutine you then try to append another six characters.  Well,
the first string is not automatically adjusted.  You might want to
return another string from your subroutine like this.

char * mstrcat(char *s, char *t)
{
   char *s2;
   s2 = (char *) malloc(strlen(s)+strlen(t)+1);  /* Extra byte for EOS */
   strcpy(s2,s);
   strcat(s2,t);
   return s2;
}

In your main program you would use:

   ....
   char *ms3;
   .....
   ms3 = mstrcat(ms1,ms2);
   print("After mstrcat ms3 = %s\n",ms3);
   ....

Hope this helps.

Terry

- Raw text -


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