Mail Archives: djgpp/1996/07/08/21:33:00
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 <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;
:
: };
: }
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 *
************************************************
- Raw text -