GDI+ samples and malloc/free [modified]
-
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
-
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
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
-
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
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.
-
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