Mail Archives: djgpp/1997/08/30/18:17:45
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 -