delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/23/13:00:12

From: raynee AT my-deja DOT com
Newsgroups: comp.os.msdos.djgpp
Subject: Re: GPF caused by string functions...
Date: Sat, 23 Sep 2000 16:43:59 GMT
Organization: Deja.com - Before you buy.
Lines: 88
Message-ID: <8qimkb$5n2$1@nnrp1.deja.com>
References: <8qi4hm$ism$1 AT nnrp1 DOT deja DOT com> <969715719 DOT 727190 AT shelley DOT paradise DOT net DOT nz>
NNTP-Posting-Host: 195.92.194.103
X-Article-Creation-Date: Sat Sep 23 16:43:59 2000 GMT
X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
X-Http-Proxy: 1.1 www-cache.pol.co.uk (NetCache 4.1R5D2), 1.1 x61.deja.com:80 (Squid/1.1.22) for client 62.136.20.55, 195.92.194.103
X-MyDeja-Info: XMYDJUIDraynee
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Thanks very much. I think I get it now... the problem is that I am not
terminating buf[] with a null character.

Silly bug, but then that's us newbies for you.

Regards,

Ed

In article <969715719 DOT 727190 AT shelley DOT paradise DOT net DOT nz>,
  "Edmund Horner" <ejrh AT paradise DOT net DOT nz> wrote:
> No offense, but I think you should rewrite the function...
>
> Otherwise, I believe your GPF will be occuring in the first strcat()
line.
> That's because you don't ensure that buf is a valid string!
>
> To fix it, stick this just after the memset():
>
> buf[j] = 0;
>
> Try that.  (You may have further problems, I haven't checked.)
>
> Edmund.
>
> <raynee AT my-deja DOT com> wrote in message
news:8qi4hm$ism$1 AT nnrp1 DOT deja DOT com...
> > I am trying to get some string functions to work as part of the
> > interface for my program. I am using ScreenPutString to write to the
> > screen and my code appears to work when I call my centrestring()
> > function once it works okay. A second call, however, causes a
General
> > Protection fault no matter how I specify my parameters. The code
that I
> > am using is:
> >
> > /* Include standard DJGPP libraries */
> > #include <pc.h>
> > #include <conio.h>
> > #include <string.h>
> >
> > /*  putstring ( str, fg, bg, col, row )
> >     Calls the DJGPP function ScreenPutString from libc.a */
> > int putstring ( char *str, int fg, int bg, int col, int row ) {
> >     ScreenPutString(str, (bg<<4) | fg, col, row);
> > }
> >
> > /*  centrestring ( str, fg, bg, row, col, width )
> >     Writes the specified string to the screen, centring it as it
does */
> > int centrestring ( char *str, int fg, int bg, int col, int row, int
> > width ) {
> >     char buf[81], spacer[81];
> >     int i,j,k;
> >     i = strlen(str);
> >     j = (width - i) / 2;
> >     memset(buf,' ',--j);
> >     strcat(buf,str);
> >     memset(spacer,' ',(width-i-j));
> >     strcat(buf,spacer);
> >     buf[width] = 0;
> >     putstring(buf,fg,bg,col,row);
> > }
> >
> > /*  main
> >     Tests the code */
> > int main ( void ) {
> >     centrestring("Just one line is okay",WHITE,BLUE,0,0,80);
> >     centrestring("This should work.",WHITE,BLUE,0,1,80);
> > }
> >
> > If anyone has any ideas as to why this doesn't work I would
appreciate
> > any feedback.
> >
> > Regards,
> >
> > Ed
> >
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.

- Raw text -


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