delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/30/18:17:45

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: String formatting problems
Date: Sat, 30 Aug 1997 13:30:12 -0700
Organization: Alcyone Systems
Lines: 44
Message-ID: <340882D4.4BB9D7DB@alcyone.com>
References: <34042ABC DOT 892782F5 AT phs DOT mat-su DOT k12 DOT ak DOT us> <34056CBA DOT 152A8249 AT LSTM DOT Ruhr-UNI-Bochum DOT De> <34091844 DOT 6030548 AT news DOT bconnex DOT net>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mad Pete wrote:

> Correct.  char *chr simply points to a byte of information (or however
> big a
> char is on your system).  If you try to store more than 1 byte of
> information,
> then things can really go haywire, especially if the pointer happens
> to be
> pointing to an area close to critical OS information  :)

This statement is misleading.

The declaration

    char *p;

declares a pointer to type char which is uninitalized if it is automatic
(unless declared static, static external or external).  An uninitialized
pointer points at some random location in memory (though it is often, by
happenstance, null -- which makes debugging difficult).  If you
dereference it, you will probably crash, because it's pointing to an
area that you're not allowed to fiddle with.

If uninitialized, it doesn't matter whether you write one byte or many. 
It's not pointing to proper memory, so dereferencing it in any way is
going to cause serious problems (even if you're only dereferencing it to
see what's there).

Pointers must always be made to point to valid (in the context of your
program) memory.  This can either be done by pointing to a local
variable (but don't use the pointer after the local variable has gone
out of scope!), or by dynamically allocating memory with malloc (or
operator new in C++).  When writing to a pointer, it's always important
to remember _what_ it's pointing at, because if you overwrite the bounds
of the memory area it's pointing to (even if it's only one byte), then
you're going to have a bad time.

-- 
          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.07n/121.53.38w
                                      \
                  "War is like love; / it always finds a way."
                                    / Bertolt Brecht

- Raw text -


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