www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/08/13:31:29

Xref: news2.mv.net comp.os.msdos.djgpp:5737
From: lehmann AT mathematik DOT th-darmstadt DOT de (Alexander Lehmann)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointer-Allocation
Date: 8 Jul 1996 16:31:46 GMT
Organization: Technische Hochschule Darmstadt
Lines: 69
Message-ID: <4rrd5i$1bsa@rs18.hrz.th-darmstadt.de>
References: <52C767D10EA AT merlin2 DOT e-technik DOT uni-erlangen DOT de>
NNTP-Posting-Host: fb0404.mathematik.th-darmstadt.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Michael Schuster (Schuster AT eev DOT e-technik DOT uni-erlangen DOT de) wrote:
: Gruess Euch!

: I've written some programs with pointers and dynamic arrays, which 
: might not be very exiting, but I came to a very interesting point, 
: when using the realloc -function of libc.a.
: I defined a pointer int* p, malloc'ed and then realloc'ed some more 
: space.(see source below)
: When using  p[a]= a for initialising the pointerposition a , everything worked fine.
: 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. 
: 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?

: Michi
: -------------------------------------------- 
: /* Dyn.c 
:    Works fine in a DOS Box (Win 3.11) but doesnot work with native DOS
: */
: #include <stdio.h>
: #include <stdlib.h>
: int main(void)
:      {
:         int *p;
:         int a;
:         p=(int*) malloc(sizeof(int));
:         for (a=0;a<20;a++) {
:            p=(int*) realloc(p,sizeof(int)*(a+1));  /* resizing p*/

:            *p=a;  /* init the pointer */
:            printf(" Nummer: %d \t %d \n", a,*p);  /* debug info */

:         /* The crucial statement */  
:            if (!(p==NULL)) p=p+1; 

:          
:             };
:      }

Well, the only pointers that may be passed to realloc and free are
ones returned by malloc or realloc, so if you do p++, the new pointer
is no longer a legal malloc pointer, in which case the behaviour is
undefined. Depending on your environment, you can get protection
faults or other odd things (e.g. in real mode DOS crashing after you
leave the program).

BTW. there is another bug in the program, you shouldn't access the
contents of p before comparing the return value to NULL, but that's
not the cause of the problem, I guess.


bye, Alexander

PS. on a sidenote, books published by Data Becker were always crap, I
don't expect the C book to be an exception. If you need a good C book,
use the K&R book 2nd edition, that's also available translated to
German.

--
Alexander Lehmann,                                  |  "On the Internet,
alex AT hal DOT rhein-main DOT de  (plain, MIME, NeXT)         |   nobody knows
lehmann AT mathematik DOT th-darmstadt DOT de (plain)          |   you're a dog."
!!CHANGED!! <URL:http://www.mathematik.th-darmstadt.de/~lehmann/>

- Raw text -


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