delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/30/21:45:38

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help - Are these pointer quirks unique to C++ or my compiler?
Date: Tue, 30 Dec 1997 18:18:20 -0800
Organization: Alcyone Systems
Lines: 43
Message-ID: <34A9AB6C.1E8925D0@alcyone.com>
References: <34A9D94B DOT 7A3D AT ns DOT sympatico DOT ca>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Visionary wrote:

>            char *newCompoundMode, tempCompoundMode;
> 
>            newCompoundMode = 0;
>            tempCompoundMode = compoundMode;
>            newCompoundMode = strlwr(&tempCompoundMode);
	...
>         I get a memory segmentation/GPF type of fault when I run it,
> but not
> when I declare them
> in the other order? Is it because we must always put pointers ahead of
> regular variables in a
> certain type declaration?

No, it's because you're writing a very poorly-behaved program; you're
lucky that it works at all.  I don't have a strlwr handy in my libraries
here (it's non-ANSI), but I presume that it takes as an argument a
NUL-terminated character string and walks through it, converting
uppercase characters to lowercase.

You are passing it the address of a character, not the location of a
NUL-terminated character string.  As such, you are invariably
read/writing inappropriate memory, which is a Bad Thing.

In this case the reason it works in one case but doesn't in the other is
because of the placement and values of these variables on the stack, and
is a "lucky" coincidence, and not something that can be relied upon.  If
you're looking into merely taking the value in `compoundMode' and
turning it into a lowercase character, you should use tolower (which
_is_ ANSI):

    char newCompoundMode;

    newCompoundMode = (char) tolower(compoundMode);

-- 
         Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                       Alcyone Systems / http://www.alcyone.com/max/
  San Jose, California, United States / icbm://+37.20.07/-121.53.38
                                     \
                       "Life may be / the product of imperfections."
                                   / (Marclo Gleiser)

- Raw text -


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