Tuesday, January 12, 2016

(C Language) (ENG/TR) Dynamic memory allocation (Dinamik hafıza tahsis etme)

ENGLISH

Actually, C language does not provide us a function for this work. However, there are functions in "stdlib.h" library about this.

malloc(), calloc(): These functions allocate space about determined size(n) for you, and return pointer. In other words, it returns an address. We can make steps for this purpose:
1)Initialize a pointer: int *ptr;
2)Get an integer n from user(or determine yourself)
3)Allocate determined memory space for pointer: ptr = (double*)malloc(n*sizeof(double));
Let's read this function from left to right: "We have double pointer. Allocate 'n times double' size for it."
After this, we can use this pointer as array! Like: ptr[0], ptr[1], ..., ptr[n - 1]
3)If we write this part again with calloc(): ptr = (double*)calloc(n,sizeof(double));
Let's read this function: "We have double type pointer. Allocate 'n times double' size for it."
Logic is the same actually. Only basic different is: 
malloc(): allocates memory about determined byte. 
calloc(): allocates determined size for array.
However, in both types, we can use this pointer as array like: ptr[0], ptr[1], ..., ptr[n - 1]
And we can write which type we want instead of double's;


When our work is done with this memory, then we should "free" this memory with free(name of pointer) function to improve performance. Because we don't want to carry "useless" weight anymore. 
Example: free(ptr);

However, we may want to change the size of pointer. That is why we use "dynamic memory allocation". The function we have to use is realloc(). I think it is relevant to "reallocation".
 ptr = realloc(ptr, new size); is the example of usage. You can write the new size as integer.




Aslında C dili bize bu iş için bir fonksiyon sunmuyor. Ama "stdlib.h" kütüphanesinde bununla ilgili fonksiyonlar mevcut.

malloc(), calloc(): bu fonksiyonlar sizin için belirli bir büyüklük(n) kadar yer ayırır ve pointer olarak return eder. Yani döndürdüğü değer bir adrestir. Kullanımını şu şekilde adımlara sokabiliriz:
1)Bir pointer initialize edin: double *ptr;
2)Kullanıcıdan bir integer(tam sayı) n değeri alın(veya siz verin)
3)Pointer'a istediğiniz büyüklükte bir bellek alanı ayırın: ptr = (double*)malloc(n*sizeof(double));

Bu fonksiyonu soldan sağa okursak: "double bir pointer'ımız var. Buna n tane double'lık yer ayır." anlamına gelir. Aslında basit bir İngilizce bilgisiyle kolayca anlaşılabilir bir yapıya sahip. Bunu tanımladıktan sonra pointer'ımızı bir array olarak kullanabiliriz: örnek: ptr[0], ptr[1], ..., ptr[n - 1]

3)Bu kısmı calloc() ile yazarsak: ptr = (double*)calloc(n,sizeof(double));

Bu fonksiyonu okuyalım: "double tipinde bir pointer'ımız var. Bu pointer'a n tane double'lık yer ayır." Mantık aynı aslında. Sadece temelindeki fark: 
malloc() kullanırken istenilen byte kadar yer ayırırız.
calloc() kullanırken array için istenilen kadar yer ayırırız. 
Ama iki türlü de ptr'mizi array olarak kullanabiliriz. 
ptr[0], ptr[1], ..., ptr[n - 1]

Ve double'lar yerlerine istediğimiz type'ı yazabiliriz.

Bu array ile işimiz bittikten sonra, performans için, hafızamızda bize yük olmasını istemeyiz. Bu yüzden hafızayı "serbest" bırakırız. Bu terim gerekli fonksiyonun özellikle aklınızda kalmasını sağlayabilir:
 free(ptr); .Yani free(pointer ismi) yazıp hafızamızı serbest bırakıyoruz.


Ama hafızamızı değiştirmek isteyebiliriz. Aslında dinamik hafızayı kullanmamızın nedeni de budur. Kullanmanız gereken fonksiyon realloc() tur. Yani ingilizce "reallocation" (yeniden tahsis) kelimesiyle alakalıdır sanıyorum. 
ptr = realloc(ptr, yeni alan); 
bu kullanımın bir örneğidir. "yeni alan" yerine girmek istediğiniz yeni alanı yazabilirsiniz.

No comments:

Post a Comment