Date: Mon, 8 Jul 1996 19:06:37 +0200 (IST) From: Eli Zaretskii To: Michael Schuster Cc: djgpp AT delorie DOT com Subject: Re: Pointer-Allocation In-Reply-To: <52C767D10EA@merlin2.e-technik.uni-erlangen.de> Message-Id: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On Mon, 8 Jul 1996, Michael Schuster wrote: > I've never had an error. But when using *p=a; p=p+1; > there was an error. (I did realloc the pointer before p+1 of course)- > My super C- book (C-Grundlagen, Data Becker) told that this should > cause no error. Well, that book is *wrong*, at least if it presents the same source that you posted. `realloc' needs the same pointer that was returned by `malloc' or previous `realloc'. But your loop sets p = p + 1, so p no longer points to the same place as it did when `malloc' returned it. Therefore, the next `realloc' fails and returns a NULL pointer. However, your program never tests for a NULL pointer before dereferencing it in *p = a, so it crashes because of NULL pointer dereference. > Somehow I had the idea to run it in a DOS Box (Win3.11) and there it > works now (see source-code). My conclusion is, that this must be the > DPMI server. > My question: > Which one behaves correctely? Or is a known thing which I' ve nerver > heard of? CWSDPMI (which is what you use under DOS, judging by the symptoms), is the one that's behaving correctly. It includes some DPMI 1.0 extensions, one of which is the NULL pointer dereference protection, for the cases like your program. The DJGPP FAQ list explains this in more details in section 9.1 (download the file v2/faq200b.zip from the same place you get DJGPP, if you don't already have the FAQ).