www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/18/19:33:21

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointers and DJGPP
Date: Wed, 18 Mar 1998 19:21:28 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 46
Message-ID: <35106508.1B35@cs.com>
References: <35103FD5 DOT A9F59A26 AT mail DOT ucsm DOT edu DOT pe>
NNTP-Posting-Host: ppp239.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

rpinnell AT characato DOT ucsm DOT edu DOT pe wrote:
> 
[code snipped]
> 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.

This is a general C question and is not really applicable to this
forum.  When you declare your strings as arrays without a defined size,
like here:

> char ms1[]="Hello";
> char ms2[]=" World";

The compiler will only allocate enough space to store the string plus
the trailing null character.  In your concatenate function, you write
onto the end of the first string, thereby exceeding the available space
and overwriting who-knows-what other data in memory.  Most likely what
gets overwritten is the stack frame of main(), and so your program
graciously continues to display the string and then crashes the instant
it leaves main().

The moral:  Always create enough space to store the results of a
concatenation.

As for general pointer problems, 99% of them can be traced to the
following causes:

 - Dereferencing an uninitialized or NULL pointer.
 - Attempting to write beyond the bounds of an allocated block of memory
(e.g., an array).
 - Assigning to a pointer variable something it wasn't intended to hold.
 - Improperly allocating memory (using sizeof(struct foo *) instead of
sizeof(struct foo), not checking for NULL return value, etc.).

Hope this helps!

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |     mailto:fighteer AT cs DOT com     |
| Plan:  To find ANYONE willing to |   http://www.cs.com/fighteer/  |
| play Descent 2 on DWANGO!        |       ICQ UIN#:  7406319       |
---------------------------------------------------------------------

- Raw text -


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