Giriş
C++ dilinde programlarken malloc yerine genellikle new() kullanılır. Ancak yine de C kütüphanesinde bulunan malloc() ve türevlerinin nasıl çalıştığını bilmekte fayda var.
kalloc - Linux'ta bulunur
Linux kernel'in kodlarında bellek ayırmak için
kullanılır.
alloca - standart C metodu değildiralloca metodu yazısına taşıdım
malloc - Standart C Metodu
Metodun imzası
şöyle.
void * malloc (size_t size);
size_t unsigned bir tiptir. size_t yerine int kullanırsak yanlış sonuçlar elde edebiliriz. Şöyle bir kod olsun. 32 bit makinelerde çıktı olarak "malloc failed"
alırız.
float *ls;
int num = 56120;
ls = (float *)malloc((num * num)*sizeof(float));
if(ls == NULL){
cout << "malloc failed !!!" << endl;
}
cout << "malloc succeeded ~~~" << endl;
Sebebi ise num * num işleminin eksi bir sayıya dönüşmesi.
-4582051584
Bunu da size_t'ye çevirince çok büyük bir sayı ortaya çıkıyor. num size_t olarak tanımlansa daha iyi.
18446744069127500032
libc malloc alternatifleri
malloc ve sistem çağrısı
Yazının bu kısmı aslında tamamen malloc gerçekleştirimine bağlı. gcc gerçekleştiriminde mmap çağrısı brk() ve mmap() kullanır. Açıklaması
şöyle I traced Linux system calls and found that if I use malloc to request a small amount of heap memory, then malloc calls brk internally.
But if I use malloc to request a very large amount of heap memory, then malloc calls mmap internally.
Tabi bu gerçekleştirimi değiştirmesi zor. Açıklaması
şöyle
The malloc implementation I suspect you are looking at (the one in the GNU C Library, based on your tags) is very old and mainly continues to be used because nobody is brave enough to take the risk of swapping it out for something newer that will probably but not certainly be better.
1. brk() ve sbrk()
Unix ve türevlerinde
brk() yanında
sbrk() sistem çağrısı da vardır. brk() ile alınan bellek genelde geri verilemiyor. Açıklaması
şöyle
brk changes the ending address of a single, contiguous "arena" of virtual address space: if this address is increased it allocates more memory to the arena, and if it is decreased, it deallocates the memory at the end of the arena. Therefore, memory allocated with brk can only be released back to the operating system when a continuous range of addresses at the end of the arena is no longer needed by the process.
2. mmap()
M_MMAP_THRESHOLD değerinden büyük malloc istekleri mmap ile karşılanır. Açıklaması
şöyle.
For allocations greater than or equal to the limit specified (in bytes) by M_MMAP_THRESHOLD that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2).
Allocating memory using mmap(2) has the significant advantage that the allocated memory blocks can always be independently released back to the system. (By contrast, the heap can be trimmed only if memory is freed at the top end.) On the other hand, there are some disadvantages to the use of mmap(2): deallocated space is not placed on the free list for reuse by later allocations;
Linux ve Overcommit
malloc'a verilen parametre
İstenilen byte sayısı 0 ise sonuç malloc gerçekleştirimine
bağlı (implementation dependent).
If the space cannot be allocated, a null pointer is returned. If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
Yani null veya bir bellek alanı
dönebilir.
If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
Fakat isterer null dönsün ister bellek alanı dönsün şu kod parçasının sonucu rahatlıkla free metoduna verilebilir. Açıklaması
şöyle.
The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation. If ptr is a null pointer, no action shall occur. Otherwise, if the argument does not match a pointer earlier returned by a function in POSIX.1-2008 that allocates memory as if by malloc(), or if the space has been deallocated by a call to free() or realloc(), the behavior is undefined.
Şöyle
yaparız.
int* x = (int*) malloc(0);
...
free(x);
FreeBSD bir bellek alanı dönüyor. Kodu
şöyle
void *
je_malloc(size_t size)
{
void *ret;
size_t usize JEMALLOC_CC_SILENCE_INIT(0);
if (size == 0)
size = 1;
[...]
Apple'ın libmalloc bir byte büyüklüğünde bellek alanı dönüyor. Kodu
şöyle
void *
szone_memalign(szone_t *szone, size_t alignment, size_t size)
{
if (size == 0) {
size = 1; // Ensures we'll return an aligned free()-able pointer
[...]
GLIBC de bellek alanı dönüyor. Kodu
şöyle.
#define request2size(req) \
(((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? \
MINSIZE : \
((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
bellek alanının içeriği
Bellek
ilklendirilmemiştir.
.... The memory is not initialized. ...
Bu alanı okumaya çalışmak
tanımsızdır.
...whose value is indeterminate. ...
Tam açıklama
şöyle
The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
İşletim sistemi sayfa sayfa çalıştığı için malloc() ile istenen alanın biraz dışına taşarak yazmak uygulamanın çökmesine sebep
olmayabilir. Zaten bu C++'taki klasik bellek hatalarını ta kendisi.
int* p = malloc(sizeof(int));
p[1000] = 12;
malloc'un döndürebileceği en büyük alan
Büyüklük tanımlı
değil.
malloc ile alınan alanı geçmek
Pointer arithmetic ile malloc ile alınan alanı +1 olarak geçmek dereference yapılmadığı müddetçe sorun yaratmaz. Açıklaması
şöyle.
It is well defined if p is pointing to one past the allocated memory and it is not
dereferenced.
Şöyle
yaparız.
int a[5];
ptrdiff_t diff = &a[5] - &a[0]; // Well-defined
int *d = malloc(5 * sizeof(*d));
assert(d != NULL, "Memory allocation failed");
diff = &d[5] - &d[0]; // Well-defined
mallac ve return result
malloc null dönebilir. Sonucu kontrol etmek gerekir.
Örnek
Şöyle
yaparız.
char *block=malloc(10000);
if (block==NULL){
printf("Can't allocate memory\n");
return -1;
}
Örnek
Şöyle
yaparız.
void *safe_malloc(size_t size)
{
void *ptr = malloc(size);
if (!ptr && (size > 0)) {
perror("malloc failed!");
exit(EXIT_FAILURE);
}
return ptr;
}
malloc ve return type
malloc() ile mmap() birbirlerine çok benziyorlar. Her ikisi de sanal bir adres döndürüyor.
malloc void* döner, C dilinde bu belleği kendi değişkenimize atarken, cast yapılmaması
gerekir. Yani aşağıdaki kullanım doğru değil!
int *ptr = (int *)malloc(10 * sizeof (*ptr));
malloc ve free arasındaki ilişki
malloc döndürdüğü bellek alanının önündeki byte'lara bellek yönetimiyle ilgili bazı bilgileri
ekler. Böylece free() metoduna kaç byte silmesi gerektiğini söylemek zorunda kalmayız. Aşağıda küçük bir
örnek var.
void *my_alloc(size_t size) {
void *block = malloc(sizeof(size) + size);
*(size_t *)block = size;
return (void *) ((size_t *)block + 1);
}
void my_free(void *block) {
block = (size_t *)block - 1;
mfree(block, *(size_t *)block);
}
calloc - Standart C Metodu
calloc metodu yazısına taşıdım.
realloc - Standart C Metodu
realloc metodu yazısına taşıdım.
mtrace
malloc ile ayrılan bellek alanlarını takip etmek için kullanılır. Açıklaması
şöyle
The mtrace() function installs hook functions for the memory-allocation functions (malloc(3), realloc(3) memalign(3), free(3)). These hook functions record tracing information about memory allocation and deallocation. The tracing information can be used to discover memory leaks and attempts to free nonallocated memory in a program.
When mtrace() is called, it checks the value of the environment variable MALLOC_TRACE, which should contain the pathname of a file in which the tracing information is to be recorded. If the pathname is successfully opened, it is truncated to zero length.