Beispiele für einige "Fallen", die durch unachtsame Programmierung (in C) entstehen können. Später - mit darauf folgenden Beispielen wird gezeigt, wie man solche Fallen mit C++-Mittel aus dem Weg räumen kann.
Ueb2a.h:
// File : Ueb2a.h // #include "..\stdafx.h" #include "Ueb2a.VerketteteListe.h" int _ueb (int argc, _TCHAR* argv[]) { // Liste von ints anlegen // Positiv-Fall: alles richtig gemacht LISTE* pListe = ErzeugeListe(); InitialisiereListe(pListe); ElementAnhaengen(pListe, 4711); ElementAnhaengen(pListe, 815); ElementAnhaengen(pListe, 42); std::cout << "Liste 1:" << std::endl; for (LISTEN_ELEMENT* iter = pListe->pStart; iter != NULL; iter = iter->pNaechster) { std::cout << " " << iter->nInhalt << std::endl; } ListeFreigeben(pListe); free(pListe); // Böse 1: Liste nicht initialisiert LISTE* pListe2 = (LISTE*) malloc(sizeof(LISTE)); ElementAnhaengen(pListe2, 4711); // Böse 2: Liste ist NULL LISTE* pListe3 = NULL; ElementAnhaengen(pListe3, 4711); // Böse 3: Listenelemente werden manipuliert LISTE* pListe4 = ErzeugeListe(); InitialisiereListe(pListe4); ElementAnhaengen(pListe4, 4711); ElementAnhaengen(pListe4, 815); ElementAnhaengen(pListe4, 42); std::cout << "Liste 1 manipuliert:" << std::endl; for (LISTEN_ELEMENT* iter = pListe4->pStart; iter != NULL; iter = iter->pNaechster) { iter->nInhalt += 9999; iter->pNaechster = pListe4->pStart; std::cout << " " << iter->nInhalt << std::endl; } return 0; } // EOF
Ueb2a.VerketteteListe.h:
// File : Ueb2a.VerketteteListe.h // struct LISTEN_ELEMENT { int nInhalt; LISTEN_ELEMENT* pNaechster; }; struct LISTE { LISTEN_ELEMENT* pStart; }; LISTE* ErzeugeListe () { LISTE* pListe = (LISTE*) malloc(sizeof(LISTE)); pListe->pStart = NULL; return pListe; } void InitialisiereListe (LISTE* pListe) { pListe->pStart = NULL; } void ElementAnhaengen (LISTE* pListe, int nInhalt) { LISTEN_ELEMENT* pNeu = (LISTEN_ELEMENT*) malloc(sizeof(LISTEN_ELEMENT)); pNeu->pNaechster = NULL; pNeu->nInhalt = nInhalt; if (pListe->pStart == NULL) { pListe->pStart = pNeu; } else { LISTEN_ELEMENT* pLetztes = pListe->pStart; while (pLetztes->pNaechster != NULL) pLetztes = pLetztes->pNaechster; pLetztes->pNaechster = pNeu; } } void ElementFreigeben (LISTEN_ELEMENT* pElement) { if (pElement->pNaechster != NULL) ElementFreigeben(pElement->pNaechster); free(pElement); } void ListeFreigeben (LISTE* pListe) { ElementFreigeben(pListe->pStart); } // EOF
Programm.cpp: - Der Einstiegspunkt
// Programm.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Ueb2a.h" int _tmain (int argc, _TCHAR* argv[]) { return _ueb(argc, argv); } // EOF
stdafx.h:
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> // TODO: reference additional headers your program requires here #include <malloc.h> #include <iostream> // EOF
targetver.h:
// Including SDKDDKVer.h defines the highest available Windows platform. // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. #pragma once #include <SDKDDKVer.h>
Stand: 19.09.2014 — : Jürgen Kreick
EOF