korrekte Verwendung von malloc und free



  • Hallo,

    ich habe jetzt etwas gesucht, hänge aber etwas mit dem folgenden Problem: Ich habe folgende Typen definiert:

    typedef struct _RGB
    {
      UCHAR r;
      UCHAR g;
      UCHAR b;
    } RGB, * PTR_RGB;
    
    typedef struct _KLT_Image
    {
      UINT cols;
      UINT rows;
      RGB* rgb;
    } KLT_IMAGE, * PTR_KLT_IMAGE;
    

    Jetzt lese ich ein Bild aus einer Datei aus und möchte dies in dem o.g. Typen ablegen. Verwende ich die Typen im unten stehenden Code korrekt?

    PTR_KLT_IMAGE img = ( PTR_KLT_IMAGE )malloc( sizeof( KLT_IMAGE ) );
    img->cols = 640;
    img->rows = 480;
    img->rgb = ( RGB* )malloc( img->cols * img->rows * sizeof( RGB ) );
    
    /* do stuff */
    
    free( img->rgb );
    free( img );
    

    Gruss. mbu.



  • Bis auf ➡ Mallo c und casten IMHO: ja.

    greetz, Swordfish


Anmelden zum Antworten