Tutorial by Examples: allo

The stackalloc keyword creates a region of memory on the stack and returns a pointer to the start of that memory. Stack allocated memory is automatically removed when the scope it was created in is exited. //Allocate 1024 bytes. This returns a pointer to the first byte. byte* ptr = stackalloc byte...
Python's str type also has a method for replacing occurences of one sub-string with another sub-string in a given string. For more demanding cases, one can use re.sub. str.replace(old, new[, count]): str.replace takes two arguments old and new containing the old sub-string which is to be replace...
A quick way to make a copy of an array (as opposed to assigning a variable with another reference to the original array) is: arr[:] Let's examine the syntax. [:] means that start, end, and slice are all omitted. They default to 0, len(arr), and 1, respectively, meaning that subarray that we are ...
6 ES6's Object.assign() function can be used to copy all of the enumerable properties from an existing Object instance to a new one. const existing = { a: 1, b: 2, c: 3 }; const clone = Object.assign({}, existing); This includes Symbol properties in addition to String ones. Object rest/sp...
from collections import Counter c = Counter(["a", "b", "c", "d", "a", "b", "a", "c", "d"]) c # Out: Counter({'a': 3, 'b': 2, 'c': 2, 'd': 2}) c["a"] # Out: 3 c[7] # not in the list (7 oc...
You can overload the function call operator (): Overloading must be done inside of a class/struct: //R -> Return type //Types -> any different type R operator()(Type name, Type2 name2, ...) { //Do something //return something } //Use it like this (R is return type, a and b a...
A a pointer to a piece of memory containing n elements may only be dereferenced if it is in the range memory and memory + (n - 1). Dereferencing a pointer outside of that range results in undefined behavior. As an example, consider the following code: int array[3]; int *beyond_array = array + 3; ...
The first operand must be a function pointer (a function designator is also acceptable because it will be converted to a pointer to the function), identifying the function to call, and all other operands, if any, are collectively known as the function call's arguments. Evaluates into the return valu...
For security reasons, PowerShell is set up by default to only allow signed scripts to execute. Executing the following command will allow you to run unsigned scripts (you must run PowerShell as Administrator to do this). Set-ExecutionPolicy RemoteSigned Another way to run PowerShell scripts is t...
Arrays can have the allocatable attribute: ! One dimensional allocatable array integer, dimension(:), allocatable :: foo ! Two dimensional allocatable array real, dimension(:,:), allocatable :: bar This declares the variable but does not allocate any space for it. ! We can specify the bounds...
In a nutshell, conditional pre-processing logic is about making code-logic available or unavailable for compilation using macro definitions. Three prominent use-cases are: different app profiles (e.g. debug, release, testing, optimised) that can be candidates of the same app (e.g. with extra log...
Standard Allocation The C dynamic memory allocation functions are defined in the <stdlib.h> header. If one wishes to allocate memory space for an object dynamically, the following code can be used: int *p = malloc(10 * sizeof *p); if (p == NULL) { perror("malloc() failed");...
NSArray *myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; // Convert myColors to mutable NSMutableArray *myColorsMutable = [myColors mutableCopy];
The for keyword is also used for conditional loops, traditionally while loops in other programming languages. package main import ( "fmt" ) func main() { i := 0 for i < 3 { // Will repeat if condition is true i++ fmt.Println(i) } } play ...
You may need to expand or shrink your pointer storage space after you have allocated memory to it. The void *realloc(void *ptr, size_t size) function deallocates the old object pointed to by ptr and returns a pointer to an object that has the size specified by size. ptr is the pointer to a memory b...
Sometimes, you need to work with an array while ensuring you don't modify the original. Instead of a clone method, arrays have a slice method that lets you perform a shallow copy of any part of an array. Keep in mind that this only clones the first level. This works well with primitive types, like n...
Cloning a huge repository (like a project with multiple years of history) might take a long time, or fail because of the amount of data to be transferred. In cases where you don't need to have the full history available, you can do a shallow clone: git clone [repo_url] --depth 1 The above comman...
If you want to check that a string contains only a certain set of characters, in this case a-z, A-Z and 0-9, you can do so like this, import re def is_allowed(string): characherRegex = re.compile(r'[^a-zA-Z0-9.]') string = characherRegex.search(string) return not bool(string) ...
Requirements Python (2.7, 3.2, 3.3, 3.4, 3.5, 3.6) Django (1.7+, 1.8, 1.9, 1.10, 1.11) Install You can either use pip to install or clone the project from github. Using pip: pip install djangorestframework Using git clone: git clone [email protected]:tomchristie/django-rest-framew...
To access functions and properties of nullable types, you have to use special operators. The first one, ?., gives you the property or function you're trying to access, or it gives you null if the object is null: val string: String? = "Hello World!" print(string.length) // Compile erro...

Page 1 of 6