/***************************************************************/ /* Prof. Dr. Carsten Vogt */ /* FH Koeln, Fak. 07 / Nachrichtentechnik */ /* http://www.nt.fh-koeln.de/vogt */ /* */ /* Beispielprogramm p05040100.c */ /* aus "C fuer Java-Programmierer", Hanser-Verlag */ /* */ /* Demonstriert wird die Benutzung von malloc() und free(). */ /***************************************************************/ #include #include int main(void) { float *pt; pt = (float *) malloc(sizeof(float)); if (pt==NULL) { printf("Speicherbelegung fehlgeschlagen!"); exit(-1); } printf("Inhalt des neu belegten Speichers von '*pt = ...': %.3f\n\n",*pt); *pt = 12.345; printf("Inhalt des neu belegten Speichers nach '*pt = ...': %.3f\n\n",*pt); free(pt); pt = 0; /* markiert Zeiger als ungueltig */ pt = NULL; /* aequivalent */ if (pt == NULL) printf("Nach 'pt = NULL': Zeiger nicht mehr gueltig!\n"); return 0; }