Averrous Saloom
Being an Engineer by Averrous

Being an Engineer by Averrous

Pointers and Dynamics Memory [Retrieval Essay]

Averrous Saloom's photo
Averrous Saloom
·Jun 14, 2021·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

I learned about pointer and dynamic memory from a book this morning. Pointer is a feature that allows programmers to manipulate programs in the range of memory allocation control. With pointer, a programmer can allocate new memory, retrieve the location of a data, and get and edit the data inside a location. This feature is powerful which then allows the implementation of dynamics memory.

The benefits of pointer can be simplified to three benefits. Runtime-sized data structure, resized data structure, and memory sharing. Runtime-sized data structure is referring to an array. Without pointer an array should have declared its size in the program initialization, makes uncertain if the array size will be not enough or too much, but with pointer array size can be sized in the runtime. Resized data-structure refers to linked list and another pointer-based data structure. Pointer allows programmers to implement linked list in efficient way, by assigning the next property as a memory location instead all of node data, makes a nesting hell. Memory sharing refers to function referencing system. When we passing an argument to a function, the passed argument will be copied, but with pointer, we can pass its location rather its overall data.

//deklarasi suatu variabel yang akan diisi lokasi
int * lokasi_kamu

//deklarasi suatu variabel yang akan diisi lokasi bersama dengan
//memori yang akan diisi. Hal ini membuat akses ke memori tersebut
//harus melalu pointer
int * lokasi_aku = new int

//mengakses lokasi suatu variabel
lokasi_kamu = &kamu

//mengakses memori yang ditunjuk suatu variabel pointer
*lokasi_aku = *lokasi_kamu

//fungsi yang dipass oleh argumen pointer
int oke(int & sip){
sip = 5

}
oke(kamu)

Sorry the comment is in Bahasa.

Next part is explaining about how memory matters by walking through on how it works. Simply put there are usually two memory organizing system used by a language, stacks and heaps. I’ve only read the stacks. Stacks meaning like stacking things. This process is happening in runtime. Each stack stacked is called activation record, which consist of the local variabel. Specifically in runtime, the bottom is the main program. And if there is a function called, it will stack another stack. And we cant access the main stack unless the function stack is removed. This terms remind me of functional programming that I learned, makes me understand more how recursive works, it stack each other.

 
Share this