Evo C++ Library v0.5.1
Unsafe Pointer Referencing

Some Evo classes use Pointer Referencing to copy without allocating memory, improving performance.

This can be unsafe (dangerous) in certain situations so documentation for classes/methods that do this will have a warning and a link to this page.

Definition

Unsafe Pointer Referencing means an object is using (referencing) a pointer it does not own that may become invalid. If the actual owner frees this pointer then the object referencing it will be invalidated, and dereferencing the pointer will crash. You're safe as long as the object referencing the pointer is cleared/destroyed before the pointer is invalidated. If the pointer is never freed (ex: string literal) then this is always safe.

Methods

The following classes have certain methods that use this technique:

This technique is used by method overloads that take a raw pointer:

Safety:

Classes

The following classes rely on Pointer Referencing:

Safety:

Read-Only Arguments

The above classes are useful for passing arguments while making the intent of passing read-only data more clear. This is especially useful in multithreaded code that may pass data between threads (with approproiate synchronization).

See also: String Passing

Example:

#include <evo/substring.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
// Passing as SubString makes it clear str is read-only
void print(const SubString& str) {
c.out << str << NL;
}
int main() {
String str("test 123");
print(str);
print("test 456");
return 0;
}

Output:

test 123
test 456

This is safe as long as the function assumes referenced data will not persist.

A bad example:

#include <evo/substring.h>
using namespace evo;
struct MyData {
SubString mydata; // DANGER: Ownership isn't clear, use String instead
void store(const SubString& str) {
mydata = str; // DANGER!
}
};
int main() {
MyData data;
data.store("test 123");
return 0;
}