cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/27/22:38:37

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C Newbie - Pointers to Structures Problem
Date: Wed, 25 Mar 1998 23:54:30 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 70
Message-ID: <3519DF86.2573@cs.com>
References: <01bd582b$a4841700$81efd4cf AT gregbl DOT ihug DOT co DOT nz>
NNTP-Posting-Host: ppp220.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Greg Bell wrote:
> 
> to swap two sets of points over. The problem is that although the program
> (included below) works it exits from TC3 with a Null Pointer Assignment
> error and from DJGPP with a rather cryptic SIGSEV error which includes
> register values and other such stuff. I use TC3 at the technical institute
> that I attend and DJGPP at home (they are both quite similar esp. when
> using RHIDE). Anyway if anyone can enlighten me it would be much
> appreciated.

First let me point out that this is an off-topic post for this
newsgroup.  It would have been better to ask in comp.lang.c or
comp.lang.c.moderated.  However, the solution is so simple you'll kick
yourself for hours after you realize what it is.

> void swap_points(POINTPTR a, POINTPTR b) //Assign pointer types to a & b
> {
>    POINTPTR temp; //Create a temporary address holder

You declare temp as a pointer to struct co_ordinates, but you never
allocate any memory to hold the value.  Writing to an uninitialized
pointer is a very common beginner error.

>    temp->x = a->x; //Shift point a to temp
>    temp->y = a->y;
>    a->x = b->x; //Shift point b to a
>    a->y = b->y;
>    b->x = temp->x; //Shift temp to point b
>    b->y = temp->y;
> }

Now, you may ask how you could have figured this out for yourself.  The
quickest and easiest way is to let the compiler do it for you.  If you
had compiled with warnings and optimizations ('-Wall' and '-O'
switches), the compiler would have told you the following:

ptrswap.c: In function `swap_points':
ptrswap.c:47: warning: `temp' might be used uninitialized in this
function

It is always a good idea to compile with '-Wall' and '-O' to catch this
sort of error.

The other way is to use that cryptic information that the program spews
out to locate where the crash occurred.  It's called a crash traceback,
because it displays all the register and stack values at the time of the
crash.  DJGPP comes with a program called 'symify' which can read such a
traceback while it's still on the screen and display the file, function,
and line number for each stack frame.  Note that in order for this to
have meaningful results, you should compile with the '-g' flag to add
full debugging information to your executable.

There is one additional problem in your code:  main MUST be declared to
return an integer according to the rules of ANSI C.  '-Wall' causes gcc
to report this problem as well.

While basic C programming is not on-topic for this newsgroup, debugging
DJGPP programs is.  I highly recommend that you obtain the DJGPP
Frequently Asked Questions list and read the chapter on debugging
(chapter 12).

hth!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "History does not record anywhere at |
|       aka Fighteer I       | any time a religion that has any     |
|   mailto:fighteer AT cs DOT com   | rational basis."                     |
| http://www.cs.com/fighteer |                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


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