Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  When and Where to Use Pointers in Go

When declaring variables in Go, we usually have two syntax options: In some scenarios, pointers; in others, reference; sometimes, either. It’s great to have choices, but it is also confusing sometimes as to which one in which scenario.To be more reasonable in choice-making, I started from pointers, walked through their natures, and summarized some rules in using them in Go.from unsplash, Jordan LadikosPointersGo has pointers. A pointer holds the memory address of a value.— from A Tour of GoThe data is stored in the memory when the program runs and each has a num...

2,272 0       POINTER GOLANG


  The Go Pointer Magic

Go is a language with the pointer type, by which we canPass pointer into a function and update value in-place.Add methods to a struct as (* T) A, which is different from (T) A().However, the pointer is type-safe in Go, meaning that there are such restrictions of the pointer.Different types of pointers are unconvertible.Pointer type cannot be used for calculation.Pointer types cannot be compared, either == nor !=.No mutual assignment between different pointer-type variables.For example, *int cannot be converted to *int32in the following code.func main() {...

3,860 0       GOLANG POINTER UNSAFE


  Some tricks and tips for using for range in GoLang

GoLang provides two major ways to loop through elements of array, slice and map. They are for and for range. Many people find that for range is very convenient when don't care about the index of the element. In this post, some tricks and tips would be talked about regarding for range.1. Loop and get pointer of each elementAssume there is a code snippet like below which is to get the pointer of each element in an array and create a new array with the corresponding pointer.arr := [2]int{1, 2}res := []*int{}for _, v := range arr { res = append(res, &v)}//expect: 1 2fmt.Println(*res[0],*res...

28,887 0       FOR RANGE GOLANG FOR LOOP POINTER


  Function Pointers in C are Underrated

The function pointer in C is, in my opinion, one of the most ignored gems of the language. It’s the kind of feature you rarely need, and then suddenly, one day, you find yourself in dire need of it, as evidenced by the real-life use-case below.If you don’t know what a function pointer is in the first place, here’s the meat of it: it gives you the ability to pass a function around like a normal variable. If you know Python / Ruby / Lisp, you might know it by the name ‘lambda’, or if you come from a JavaScript background, you might’ve used it under the na...

4,309 0       C ANALYSIS POINTER


  C/C++ Pointer Declaration Syntax – It makes sense!

I never really liked the way pointers are declared in C/C++:int *a, *b, *c; // a, b and c are pointers to intThe reason is that I am used to reading variable declarations as MyType myVar1, myVar2, myVar3; and I always read “int*” as the type “integer pointer”. I therefore wanted the followingint* a, b, c; // a is a pointer to int, b and c are intsto mean that a, b and c all were of type int*, i.e. pointers to int. and I therefore found it slightly annoying to repeat the asterisk for every variable. This also meant that the symbol * had two sligh...

2,760 0       C POINTER DECLARATION ATTEMPT


  Smuggling data in pointers

While reading up on The ABA Problem I came across a fantastic hack.  The ABA problem, in a nutshell, results from the inability to atomically access both a pointer and a "marked" bit at the same time (read the wikipedia page).  One fun, but very hackish solution is to "smuggle" data in a pointer.  Example:#include "stdio.h"void * smuggle(void * ptr, int value){  return (void *)( (long long)ptr | (value & 3) );}int recoverData(void * ptr){  return (long long)ptr & 3;}void * recoverPointer(void * ptr){  return (void *)( (long long)ptr & (~3) );}int main(...

3,035 0       C DATA POINTER BIT ATOMIC SMUGGLE