Tuesday, April 1, 2008
Detect Heap Corruption -- Using CrtCheckMemory()
In the search for memory validation APIs I have found _CrtCheckMemory() API which is being used by debug version of C run-time libraries. Using this API wrapped with an assert macro gives us the opportunity to check the heap every few lines of code without any overhead in release version: _ASSERTE( CrtCheckMemory());
it would be smart idea to use it even without any visible corruption, it saves time in the long run . This tip is based on information written on the MSDN.
Tuesday, March 25, 2008
Forcing Cpp Functions To Be Called Non-inlined
Often such little functions are auto-inlined by the optimizer, when compiling in release/optimized mode. Although there is a way to prevent function from inlining by prototyping it with the _cdecl keyword, sometimes all you need just specific calls to be non-inlined.
Here is the macro:
#define NON_INLINED_CALL(a) (((int)(a)+1)?(a):(0))
Now, by forcing some fake calculation (int(a)+1) we stop the compiler from optimizing given line, also by using v ? (a) : (0), we are forcing the compiler to use the _cdecl way of calling the function.
Example:
static int test(int a, int b, int c)
{
return a*b*c;
}
#define NON_INLINED_CALL(a) (((int)(a)+1)?(a):(0))
extern int results[2];
extern int a, b, c;
extern int d, e, f;
void test2( void )
{
results[0] = test(a,b,c); // Eventually with good optimizations this will be inlined
results[1] = NON_INLINED_CALL(test)(d,e,f); // No inlining here
}
Under MSVC we can construct simple and faster macro
#define NON_INLINED_CALL_MSVC(a) ((a) ? (a) : 0)
but the latter will not stop GCC optimizer, and we'll not get the results we wanted to.
P.S. I didn't test any other compilers - only GCC 2.99(ps2-ee one), and MSVC6.00
Monday, March 24, 2008
Preventing object creation on stack
Yes!!!
What you should do is to make your destructor private, rather than public or protected.
You can do like this:
class CTest {
private:
~CTest ();
}
Why only private, because if you make it protected, yes the object this class can't not be created on stack, but it's derived class can!
Then, if you want to create the object on stack, like
CTest test;
The compiler is gona complain, the error message is most probably like 'CTest::~CTest' : cannot access private member declared in class CTest'.
That's because the compiler will actually check if it can delete the object if it's created on stack, because it's compiler's business to take care of this. If you do like:
CTest* test
or
CTest* test = new CTest
It's gona be fine, because the only thing you put on the stack is just the pointer, compiler don't even have to call the destructor of the pointer.
Then, how do you delete the object whose destructor is private?? Just use delete operator is not going to work, because delete operator is not a part of the CTest class. So you have to provide a function which will delete the object itself.
It's like:
void CTest::Release()
{
delete this;
}
Since "delete this" is not a "good manner"(it works, but it's not recommended), you have to pay more attention when you use this. You have to make sure that the object is not created by "new[]". You have to make sure Release is the last function you call on this object, etc.
Wednesday, November 28, 2007
'Google's Gdrive on Way'
Google Inc is preparing a service that would enable users to store data from their personal hard drives on its computers, the Wall Street Journal reported Tuesday in its online edition.
Users would be able to house files they would normally store on personal computers -- such as word-processing documents, digital music, video clips and images -- on Google's computers, the Journal said, citing sources familiar with the matter.
According to the Journal, the service could let users access their files through the Internet from different computers and mobile devices when they sign on using a password.
The service could be released as soon as a few months from now, the Journal said, citing a source.
The newspaper also said Google plans to provide some free storage, with additional storage allotments available for a fee.
Planned pricing isn't known, the Journal added.
Google has responded. And it insists on using that silly cloud computing moniker:
"Cloud computing is going mainstream. The apps people use every day, such as email, photo sharing, and word processing, are moving to the web because it's easier to share and access your data from anywhere when it's online, in one place.
Storage is an important component of making web apps fit easily into consumers' and business users' lives. That's why we've always offered a lot of free storage, and it's why we offer paid options for buckets of 'overflow' storage spanning across apps, including Picasa Web Albums and Gmail for now, with more services like Google Docs to come.
We're always listening to our users and looking for ways to update and improve our web applications, including storage options, but we don't have anything new to announce right now."
Of course, we all know that sooner or later, GDrive will arrive.
Thursday, August 23, 2007
Wikiscanner.
Ever heard of a Wikipedia reconnaissance tool?
Now you can with the Wikiscanner list anonymous wikipedia edits from interesting organizations.
You see, that is why Wikipedia rocks.
Try it out