September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,4 @@
var x = 12
var pointer = ptr(x) # get pointer
print pointer # print address
pointer.set 0xFFFE # set the address

View file

@ -1,5 +1,6 @@
void test(ref int i) {
writefln(&i);
import std.stdio;
writeln(&i);
}
void main() {

View file

@ -0,0 +1,10 @@
// Kotlin/Native Technology Preview
import kotlinx.cinterop.*
fun main(args: Array<String>) {
val intVar = nativeHeap.alloc<IntVar>()
intVar.value = 42
with(intVar) { println("Value is $value, address is $rawPtr") }
nativeHeap.free(intVar)
}

View file

@ -1,3 +1,33 @@
declare addr builtin; /* retrieve address of a variable */
declare ptradd builtin; /* pointer addition */
declare cstg builtin; /* retrieve length of the storage of a variable */
declare hbound builtin; /* retrieve the number of elements in an array */
declare p pointer;
k = addr(b); /* Obtain address of variable, stored in integer variable k */
p = addr(q); /* assigns address to pointer variable p. */
declare i bin fixed(31) init(42);
p = addr(i); /* Obtain address of variable, stored in integer variable k */
/* how to read a string bit by bit - example for pointerAdd */
/* we built a pointer (movingPointer), which will move through the */
/* storage of a variable (exampleTxt). attached to the pointer is */
/* an array of bits (movingBit) - this means wherever the pointer */
/* is pointing to, this will also be the position of the array. */
/* only whole bytes can be addressed. to get down to the single bits, */
/* an array of 8 bits is used. */
declare exampleTxt char(16) init('Hello MainFrame!);
declare movingPointer pointer;
declare movingBit(8) bit(01) based(movingPointer);
declare walkOffset bin fixed(31);
declare walkBit bin fixed(31);
do walkOffset = 0 to cstg(exampleTxt)-1;
movingPointer = ptradd(addr(exampleTxt, walkOffset);
do walkBit = 1 to hbound(movingBit);
put skip list( 'bit at Byte ' !!walkOffset
!!' and position '!!walkBit
!!' is ' !!movingBit(walkBit));
end;
end;

View file

@ -1,2 +1,11 @@
let var = 1;
println!("address of var: {:p}", &var);
let v1 = vec![vec![1,2,3]; 10];
println!("Original address: {:p}", &v1);
let mut v2;
// Override rust protections on reading from uninitialized memory
unsafe {v2 = mem::uninitialized();}
let addr = &mut v2 as *mut _;
// ptr::write() though it takes v1 by value, v1s destructor is not run when it goes out of
// scope, which is good since then we'd have a vector of free'd vectors
unsafe {ptr::write(addr, v1)}
println!("New address: {:p}", &v2);

View file

@ -1,5 +1,2 @@
let address: usize = 0x7ffc8f303130;
unsafe {
let val = *(address as *const usize);
println!("value at {}: {:?}", address, val);
}
let var = 1;
println!("address of var: {:p}", &var);

View file

@ -1,3 +1,5 @@
let address: usize = 0x7ffc8f303130;
unsafe {
*(0x7ffc8f303130 as *mut usize) = 1;
let val = *(address as *const usize);
println!("value at {}: {:?}", address, val);
}

View file

@ -0,0 +1,5 @@
unsafe {
*(0x7ffc8f303130 as *mut usize) = 1;
// Note that this invokes undefined behavior if 0x7ffc8f303130 is uninitialized. In that case, std::ptr::write should be used.
std::ptr::write(0x7ffc8f303130 as *mut usize, 1);
}

View file

@ -1,31 +1,29 @@
class MyClass {
}
class MyClass { }
func printPointer<T>(ptr: UnsafePointer<T>) {
println(ptr)
func printAddress<T>(of pointer: UnsafePointer<T>) {
print(pointer)
}
func test() {
var x = 42
var y = 3.14
var z = "foo"
var obj = MyClass()
var x = 42
var y = 3.14
var z = "foo"
var obj = MyClass()
// Use a pointer to a variable on the stack and print its address
withUnsafePointer(&x) { ptr in println(ptr) }
withUnsafePointer(&y) { ptr in println(ptr) }
withUnsafePointer(&z) { ptr in println(ptr) }
withUnsafePointer(&obj) { ptr in println(ptr) }
// Use a pointer to a variable on the stack and print its address.
withUnsafePointer(to: &x) { print($0) }
withUnsafePointer(to: &y) { print($0) }
withUnsafePointer(to: &z) { print($0) }
withUnsafePointer(to: &obj) { print($0) }
// Alternately:
printPointer(&x)
printPointer(&y)
printPointer(&z)
printPointer(&obj)
// Alternately:
printAddress(of: &x)
printAddress(of: &y)
printAddress(of: &z)
printAddress(of: &obj)
// Printing the address of an object that an object reference points to
// In Swift 3, unsafeAddress is removed
println(Unmanaged.passUnretained(obj).toOpaque())
// Printing the address of an object that an object reference points to.
print(Unmanaged.passUnretained(obj).toOpaque())
}
test()