Advertisement

Get name of a primitive passed as parameter to C++

Started by July 04, 2012 05:24 PM
9 comments, last by WitchLord 12 years, 5 months ago
Indeed, the references are no more 'unsafe' than references are in C++. Badly written C++ code can also crash the application, but with proper testing you avoid that.

It's mostly a question of how far you're willing to go to keep the scripts in a sandbox, i.e. do you trust the script writers to properly test the scripts and not willfully attempt to sabotage the application?

Just to put things in perspective, here's an example where an unsafe reference could possibly cause a crash:


array<int> arr = {1,2,3};
int DoSomething(int& refToInt)
{
// Resize the global array, which will deallocate the
// old internal buffer that our reference points to
arr.resize(100);

// Do some more stuff

// Change the value of the refToInt
refToInt = 42; /// <--- Oops, the reference is no longer pointing to the array element
}
void main()
{
// Call the function with a reference to one of the elements in the array
DoSomething(arr[0]);
}


Depending on exactly what was allocated on the heap where the array was originally allocated, the update that is made to the reference may be harmless, corrupt the heap, or overwrite some other pointer, etc.

Without unsafe references, the compiler guarantees that an error like this cannot be made (willfully or not).

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement