Xref: news2.mv.net comp.os.msdos.djgpp:5747 From: dmicolet AT u-bourgogne DOT fr (Dominique Micollet) Newsgroups: comp.os.msdos.djgpp Subject: Re: Pointer-Allocation Date: 8 Jul 1996 16:52:09 GMT Organization: Universite de Bourgogne - Dijon - France Lines: 68 Message-ID: <4rrebp$qi1@tempo.univ-lyon1.fr> References: <52C767D10EA AT merlin2 DOT e-technik DOT uni-erlangen DOT de> NNTP-Posting-Host: satie.u-bourgogne.fr 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: : /* Dyn.c : Works fine in a DOS Box (Win 3.11) but doesnot work with native DOS : */ : #include : #include : 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; : : }; : } According to me, this can not work. malloc, free and realloc use an internal memory manager which stores the size of the allocated area somewhere relative to the actual value of the pointer (p here) : the most common way is just before p. As realloc is basically a free followed by a malloc , when you do p=p+1, the free function will be completely fooled (it will use p[0] as the size to free of something like that). I don't understand how this program works in a DOS Box under Win3.11 : ********************************************** : Michael Schuster : E-mail: Schuster AT eev DOT e-technik DOT uni-erlangen DOT de : Universitaet Erlangen/Nuernberg : Lehrstuhl fuer Elektrische Energieversorgung : Cauerstrasse 4 : 91058 Erlangen : http://www.eev.e-technik.uni-erlangen.de/ : ********************************************** -- Cordialement ************************************************ * Dominique MICOLLET * * Laboratoire LIESIB * * Universite de Bourgogne * * 6 Boulevard GABRIEL * * BP 138 * * 21004 DIJON CEDEX * * FRANCE * * Tel : /33/80-39-59-27 * * Tfx : /33/80-39-60-43 /33/80-39-59-99 * * Email : Dominique DOT Micollet AT u-bourgogne DOT fr * ************************************************