Use safe checking mechanism

I

Immortal Nephi

I use class to create some objects. Objects such as line, circle,
and other shapes are drawn on the screen.
main function has its own responsibility to allocate Pixel Buffer
into memory and release it from memory when Test class is finished.
However, Test class needs to access Pixel Buffer directly. If either
Draw_Line function or Draw_Circle function attempt to access unknown
memory, then the eror message is triggered by operating system to tell
execution violation or security denied.
I decided to place default constructor to private. The constructor
with parameter is required.
What happened if pointer to address memory variable in Draw_Line
function does not have memory allocation? You will get the same
message from operating system. You need to use safe checking
mechanism.
Each function requires to test if memory is already allocated or
not. It will return to false, otherwise allows to execute and return
true.
I will create multiple objects from one Test class and multiple
objects are drawn on the screen. Please let me know if safe checking
mechanism is best practice.

Typedef unsigned short TUWord;

class Test
{
public:
Test ( TUWord* pui_PixelBuffer ) : pmui_PixelBuffer
( pui_PixelBuffer ) {}

~Test() {}

bool Draw_Line( TUWord X, TUWord Y, TUWord width )
{
if( pmui_PixelBuffer == 0 )
return false;

// Do something to draw
// Fill RGB data into Pixel Buffer
return true;
}

bool Draw_Circle( TUWord X, TUWord Y )
{
if( pmui_PixelBuffer == 0 )
return false;

// Do something to draw
// Fill RGB data into Pixel Buffer
return true;
}

private:
Test() {}
Test( const Test & ) {}
Test &operator=( const Test & ) {}

TUWord* pmui_PixelBuffer;
}; // end class Test

int main()
{
TUWord* Pixels = new TUWord [ 1024 * 1024 ];

// Test private_t; // Error private constructor
Test t( Pixels ); // Grant constructor with parameter

t.Draw_Line( 10, 10, 150 );
t.Draw_Circle( 50, 50 );

delete [] Pixels;

return 0;
} // end function main
 
A

Alf P. Steinbach

* Immortal Nephi:
Each function requires to test if memory is already allocated or
not. It will return to false, otherwise allows to execute and return
true.
I will create multiple objects from one Test class and multiple
objects are drawn on the screen. Please let me know if safe checking
mechanism is best practice.

It's not.

Typedef unsigned short TUWord;

class Test
{
public:
Test ( TUWord* pui_PixelBuffer ) : pmui_PixelBuffer
( pui_PixelBuffer ) {}

~Test() {}

bool Draw_Line( TUWord X, TUWord Y, TUWord width )
{
if( pmui_PixelBuffer == 0 )
return false;

// Do something to draw
// Fill RGB data into Pixel Buffer
return true;
}

Use a std::vector.

Apply the same design princinples to your own classes, namely, establish class
invariant in every constructor, maintain it in every member routine, never check
it dynamically.


Cheers & hth.,


- Alf
 
I

Immortal Nephi

* Immortal Nephi:


It's not.










Use a std::vector.

Apply the same design princinples to your own classes, namely, establish class
invariant in every constructor, maintain it in every member routine, never check
it dynamically.
Alf,

I try to explain how objects are drawn to the screen. Pixel Buffer is
the only one big array in memory. It contains color information such
as RGB values each element in the array. I don't need to use vector
if I want to access Pixel Buffer directly. Also, I don't need safe
checking mechanism to manipulate Pixel Buffer because one variable has
constant maximum numbers of elements.

For example:
const int kiMaxPixels = 1024 * 1024;
TUWord* piPixelBuffer = new TUWord [ kiMaxPixels ];

I want to create multiple objects. Multiple objects are like line,
circle, rectangle, and other shapes. Each object share Pixel Buffer
because each object copies shape and convert it into Pixel information
and put it into Pixel Buffer.

I use Pixel Buffer to create bitmap images.

How do you say why I should use vector? Should I create multiple
objects in vector directly? But...I want to create my own member
functions to draw any shapes. Please clarify and provide me example.
Thanks...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top