Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GDI+ samples and malloc/free [modified]

GDI+ samples and malloc/free [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
graphicswinformstutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bob16972
    wrote on last edited by
    #1

    In the MSDN source code for some GDI+ methods, the authors use malloc and free. Why would they do this if RectF is a class? Is this a hint that it is merely the equivalent to a struct with the ctor and dtor doing nada? Should we follow this convention or use new/delete instead? Here's an example for GetRegionScans()...

    VOID Example_GetRegionScansRect(HDC hdc)
    {
    Graphics graphics(hdc);
    SolidBrush solidBrush(Color(255, 255, 0, 0));
    Pen pen(Color(255, 0, 0, 0));
    GraphicsPath path;
    Matrix matrix;
    Rect* rects = NULL;
    INT count = 0;

    // Create a region from a path.
    path.AddEllipse(10, 10, 50, 300);
    Region pathRegion(&path);
    graphics.FillRegion(&solidBrush, &pathRegion);

    // Get the rectangles.
    graphics.GetTransform(&matrix);
    count = pathRegion.GetRegionScansCount(&matrix);
    rects = (Rect*)malloc(count*sizeof(Rect));
    pathRegion.GetRegionScans(&matrix, rects, &count);

    // Draw the rectangles.
    for(INT j = 0; j < count; ++j)
    graphics.DrawRectangle(&pen, rects[j]);

    free(rects);
    }

    modified on Tuesday, December 04, 2007 1:45:44 PM

    D D 2 Replies Last reply
    0
    • B bob16972

      In the MSDN source code for some GDI+ methods, the authors use malloc and free. Why would they do this if RectF is a class? Is this a hint that it is merely the equivalent to a struct with the ctor and dtor doing nada? Should we follow this convention or use new/delete instead? Here's an example for GetRegionScans()...

      VOID Example_GetRegionScansRect(HDC hdc)
      {
      Graphics graphics(hdc);
      SolidBrush solidBrush(Color(255, 255, 0, 0));
      Pen pen(Color(255, 0, 0, 0));
      GraphicsPath path;
      Matrix matrix;
      Rect* rects = NULL;
      INT count = 0;

      // Create a region from a path.
      path.AddEllipse(10, 10, 50, 300);
      Region pathRegion(&path);
      graphics.FillRegion(&solidBrush, &pathRegion);

      // Get the rectangles.
      graphics.GetTransform(&matrix);
      count = pathRegion.GetRegionScansCount(&matrix);
      rects = (Rect*)malloc(count*sizeof(Rect));
      pathRegion.GetRegionScans(&matrix, rects, &count);

      // Draw the rectangles.
      for(INT j = 0; j < count; ++j)
      graphics.DrawRectangle(&pen, rects[j]);

      free(rects);
      }

      modified on Tuesday, December 04, 2007 1:45:44 PM

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      bob16972 wrote:

      Why would they do this if RectF is a class?

      Does its constructor/destructor do anything? Keep in mind that just because it's an MSDN sample, that does not imply it's also perfect code. Folks that supply samples to MSDN are not infallible. MSDN does provide lots of great code, some of which can be used unaltered, but it should still be scrutinized regardless.

      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      B 1 Reply Last reply
      0
      • D David Crow

        bob16972 wrote:

        Why would they do this if RectF is a class?

        Does its constructor/destructor do anything? Keep in mind that just because it's an MSDN sample, that does not imply it's also perfect code. Folks that supply samples to MSDN are not infallible. MSDN does provide lots of great code, some of which can be used unaltered, but it should still be scrutinized regardless.

        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        B Offline
        B Offline
        bob16972
        wrote on last edited by
        #3

        DavidCrow wrote:

        Does its constructor/destructor do anything?

        I'm not sure the GDI+ source code is available for inspection (except the declaration files of course)

        DavidCrow wrote:

        Keep in mind that just because it's an MSDN sample, that does not imply it's also perfect code. Folks that supply samples to MSDN are not infallible. MSDN does provide lots of great code, some of which can be used unaltered, but it should still be scrutinized regardless.

        I understand what your saying. I was scrutinizing it and I couldn't determine why they were doing it. I thought I'd post the question to see if it made sense to anybody and if they could explain what benefits the author is realizing here or if the code is flawed. Thanks for the input.

        1 Reply Last reply
        0
        • B bob16972

          In the MSDN source code for some GDI+ methods, the authors use malloc and free. Why would they do this if RectF is a class? Is this a hint that it is merely the equivalent to a struct with the ctor and dtor doing nada? Should we follow this convention or use new/delete instead? Here's an example for GetRegionScans()...

          VOID Example_GetRegionScansRect(HDC hdc)
          {
          Graphics graphics(hdc);
          SolidBrush solidBrush(Color(255, 255, 0, 0));
          Pen pen(Color(255, 0, 0, 0));
          GraphicsPath path;
          Matrix matrix;
          Rect* rects = NULL;
          INT count = 0;

          // Create a region from a path.
          path.AddEllipse(10, 10, 50, 300);
          Region pathRegion(&path);
          graphics.FillRegion(&solidBrush, &pathRegion);

          // Get the rectangles.
          graphics.GetTransform(&matrix);
          count = pathRegion.GetRegionScansCount(&matrix);
          rects = (Rect*)malloc(count*sizeof(Rect));
          pathRegion.GetRegionScans(&matrix, rects, &count);

          // Draw the rectangles.
          for(INT j = 0; j < count; ++j)
          graphics.DrawRectangle(&pen, rects[j]);

          free(rects);
          }

          modified on Tuesday, December 04, 2007 1:45:44 PM

          D Offline
          D Offline
          DoomedOne
          wrote on last edited by
          #4

          Hi, I gess is using malloc and free to avoid a loop for alocation a "count" number of Rect objects, alocation all of them in one malloc look at malloc(count*sizeof(Rect) `parameter of malloc.`

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups