Skip to content

Commit a41e862

Browse files
committed
Support more languages for review
1 parent 2f1be62 commit a41e862

File tree

12 files changed

+173
-0
lines changed

12 files changed

+173
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python does not have pointers like C, but we can simulate similar behavior using references
2+
3+
num = 10
4+
fnum = 3.14
5+
6+
# Simulating a void pointer behavior by storing references in a dictionary
7+
vptr = {"type": "int", "value": num}
8+
print(f"Integer: {vptr['value']}") # Output: 10
9+
10+
vptr = {"type": "float", "value": fnum}
11+
print(f"Float: {vptr['value']:.2f}") # Output: 3.14
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Simulating NULL pointer behavior in Python using None
2+
3+
ptr = None # Initializing to None (similar to NULL in C)
4+
print(f"1. Initial ptr value: {ptr}")
5+
6+
# Check for None (NULL) before using
7+
if ptr is None:
8+
print("2. ptr is None, cannot dereference")
9+
10+
# Simulate memory allocation
11+
ptr = 42 # Allocating a value to ptr
12+
print(f"4. After allocation, ptr value: {ptr}")
13+
14+
# Safe to use ptr after checking for None
15+
if ptr is not None:
16+
print(f"5. Value at ptr: {ptr}")
17+
18+
# Simulate freeing memory (set to None after use)
19+
ptr = None
20+
print(f"6. After free, ptr value: {ptr}")
21+
22+
# Safety check after 'free' (set to None)
23+
if ptr is None:
24+
print("7. ptr is None, safely avoided use after free")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python equivalent for iterating through an array-like structure
2+
3+
arr = [12, 24, 36, 48, 60]
4+
5+
# Position one (first element)
6+
print(f"Position one: {arr[0]}")
7+
8+
# Iterate over the array and simulate printing memory address
9+
for num in arr:
10+
print(f"{num}\t{id(num)}")
11+
# id() gives a unique identifier for the object, similar to a memory address
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python equivalent for simulating matrix-like behavior using lists
2+
3+
arr1 = [1, 2, 3, 4]
4+
arr2 = [5, 6, 7, 8]
5+
matrix = [arr1, arr2]
6+
7+
for row in matrix:
8+
for i in range(len(row)):
9+
print(row[i], end=" ")
10+
print()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
num <- 10
2+
fnum <- 3.14
3+
4+
# Simulating the void pointer with a list containing references
5+
vptr <- list(type = "int", value = num)
6+
cat("Integer:", vptr$value, "\n") # Output: 10
7+
8+
vptr <- list(type = "float", value = fnum)
9+
cat("Float:", sprintf("%.2f", vptr$value), "\n") # Output: 3.14
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Simulating NULL pointer behavior in R
2+
3+
ptr <- NULL # Initialize ptr as NULL
4+
cat("1. Initial ptr value:", ptr, "\n")
5+
6+
# Check for NULL before using
7+
if (is.null(ptr)) {
8+
cat("2. ptr is NULL, cannot dereference\n")
9+
}
10+
11+
# Simulate memory allocation
12+
ptr <- 42 # Assigning a value to ptr (simulating allocation)
13+
cat("4. After allocation, ptr value:", ptr, "\n")
14+
15+
# Safe to use ptr after NULL check
16+
if (!is.null(ptr)) {
17+
cat("5. Value at ptr:", ptr, "\n")
18+
}
19+
20+
# Simulate freeing memory (set to NULL after use)
21+
ptr <- NULL
22+
cat("6. After free, ptr value:", ptr, "\n")
23+
24+
# Safety check after 'free' (set to NULL)
25+
if (is.null(ptr)) {
26+
cat("7. ptr is NULL, safely avoided use after free\n")
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# R equivalent for iterating through an array-like structure
2+
3+
arr <- c(12, 24, 36, 48, 60)
4+
5+
# Position one (first element)
6+
cat("Position one:", arr[1], "\n")
7+
8+
# Iterate over the array and simulate printing memory address
9+
for (num in arr) {
10+
cat(num, "\t", pryr::address(num), "\n")
11+
# pryr::address() returns a simulated memory address in R
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# R equivalent for simulating matrix-like behavior using lists
2+
3+
arr1 <- c(1, 2, 3, 4)
4+
arr2 <- c(5, 6, 7, 8)
5+
matrix <- list(arr1, arr2)
6+
7+
for (row in matrix) {
8+
for (value in row) {
9+
cat(value, " ")
10+
}
11+
cat("\n")
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
num = 10
2+
fnum = 3.14
3+
4+
# Simulating the void pointer and dereferencing by casting in Mojo
5+
vptr = &num
6+
print(f"Integer: {*(int*)vptr}") # Output: 10
7+
8+
vptr = &fnum
9+
print(f"Float: {*(float*)vptr:.2f}") # Output: 3.14
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Simulating NULL pointer behavior in Mojo
2+
3+
var ptr: *int = null // Initialize pointer to NULL
4+
print("1. Initial ptr value: ", ptr)
5+
6+
if (ptr == null) {
7+
print("2. ptr is NULL, cannot dereference")
8+
}
9+
10+
// Simulate memory allocation
11+
ptr = alloc(int) // Allocating memory for an integer
12+
ptr! = 42 // Dereferencing and assigning a value
13+
print("4. After allocation, ptr value: ", ptr)
14+
15+
if (ptr != null) {
16+
print("5. Value at ptr: ", ptr!)
17+
}
18+
19+
// Simulate freeing memory (set to NULL after use)
20+
ptr = null
21+
print("6. After free, ptr value: ", ptr)
22+
23+
if (ptr == null) {
24+
print("7. ptr is NULL, safely avoided use after free")
25+
}

0 commit comments

Comments
 (0)