www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/06/01:02:26

Date: Wed, 6 Nov 1996 07:51:55 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Matthew Miller <namille2 AT vt DOT edu>
Cc: djgpp AT delorie DOT com
Subject: Re: array of pointers to strings, BC++, DJGPP and weird printing behavior
In-Reply-To: <55nrvs$sdo@solaris.cc.vt.edu>
Message-Id: <Pine.SUN.3.91.961106073945.304A-100000@is>
Mime-Version: 1.0

On 5 Nov 1996, Matthew Miller wrote:

> the same.  With DJGPP, running the exe. produces an endless loop
> of seg. faults.

You have just learned from your own experience that working in a 
protected-mode environment can save you much grief and embarrassment, 
because it doesn't let you reference wild pointers as easy as real-mode 
DOS does.

Your problem is a couple of lines such as this:

> 	KeyWords = (char **) malloc ( sizeof(char) );
                                            ^^^^^^
Since you want KeyWords to be an array of pointers, you should instead do 
this:
 	KeyWords = (char **) malloc ( sizeof(char *) );
                                            ^^^^^^^^
The effect is that the array thus allocated is 1/4 of the size you want 
it to be, since in DJGPP sizeof(char *) = 4 and sizeof(char) = 1.  Then 
when you proceed to fill this array with pointers to char, you eventually 
begin to overwrite memory beyond the limits of the space allocated for 
the array.  BC lets you get away with this, but DJGPP catches this and 
crashes your program.

(There might be other problems in your program, but once I saw this one, 
I stopped looking for others.)

Btw, the stack trace printed when the program segfaults includes
information that you should use to begin debugging.  Just run the `symify'
program when the stack trace is still on the screen.  (The DJGPP FAQ list,
available as v2/faq202b.zip from the same place you get DJGPP, explains
you how to use `symify' in section 9.2.)

- Raw text -


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