From Second Life Wiki
Jump to navigation Jump to search

No, this won't destroy your original string...

A quick look at the source code for this user-contributed function might get some newbie scripters quite alarmed at the use of llDeleteSubString here, and panic that their precious string gets hopelessly chopped in the middle!

Rest assured; this will not happen. That's because LSL functions always pass "Wikipedia logo"parameters by value, and never "Wikipedia logo"by reference. That essentially means that the function will only get a local copy of the parameter, not the original parameter. Thus, all manipulations to the string occur inside the function with local copies of whatever was passed to them. LSL doesn't have the concept of passing parameters by reference — or a pointer to an object — where, indeed, you'd be manipulating the original contents stored in memory.

But languages which only pass parameters by value are much safer (there is no way end-users can overwrite memory locations of variables directly), easier for beginners, and easier to compile — and debug . Good examples are the programming languages Pascal or classic BASIC. The tradeoff is requiring more memory (since so many copies have to made during the lifetime of an object — on top of which one has a lot of overhead during function calls).

By contrast, languages such as C can also pass parameters by reference, i.e. they get a pointer to the start of the memory block where the actual contents of the original parameter are. This means that there is a huge savings in memory — and in function call overhead (many functions in C/C++ get optimised by the compiler and become inline code, i.e. they aren't 'functions' at all) — at the cost of making even the slightest programme a source of apocalyptic chaos, crashing the very kernel and bringing everything down with it :-)

That's one of the reasons most modern programming languages — such as LSL — prefer to have some overhead and more memory consumption, but give programmers/scripters a much "safer" environment to work with. You can still do a lot of chaos in LSL, though, as you all very well know.

Gwyneth Llewelyn (talk) 07:56, 15 October 2023 (PDT)