15 Jo P Mar 08

314 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
314
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

15 Jo P Mar 08

  1. 1. The Joy of Programming Dynamic Memory Allocation in C S.G. GaneSh In this column, let us try to understand the nitty-gritty of dynamic allocation using a few programming problems. A ssume that you are writing code to implement a text So, the design to store characters in the text editor by using editor in C. To represent characters typed in the malloc(sizeof(char)) is not a good idea. editor, you plan to use dynamic memory allocation Now let us look at a problem in using dynamic allocation. for each character. In the following code segment, text is The following code is for freeing a linked list. Will it work? a two-dimensional array of ‘char*’s. When the user types a character, say ‘a’, in row_pos, column_pos, memory for( ptr = head ; ptr != NULL ; ptr = ptr->next ) is dynamically allocated only for sizeof(char) and the free(ptr); character is stored as in: Here, in the expression ptr = ptr->next, ptr is accessed text[row_pos][column_pos] = malloc(sizeof(char)); after ptr is released using function free. This also serves as *text[row_pos][column_pos] = ‘a’; example for dangling pointers where the pointer is used even after freeing the block. So the behaviour of this code segment Is this an efficient approach to store characters that are is undefined. However, it usually works well in practice. Why? typed in the text editor? The free function need not actually ‘release’ the memory In this approach, there are many problems in the immediately after the call to free it. Usually, C implementations assumptions made for storing characters in a text implement ‘free’ by moving a memory block to a ‘free list’ editor. Let me point out a very significant problem. The without modifying the contents in the block. So, this code will sizeof(char) is 1. But malloc(1) does not return a memory work fine in many of the implementations. However, it is not block of size 1 byte. Why? For that we need to understand recommended to write code like this with such implementation how dynamic memory allocation works. defined behaviour. For this problem, there is a simple Generally, in C, dynamic memory allocation is solution—introduce a temporary variable as in: implemented using a linked list mechanism to allocate memory blocks of different sizes. So, for each block for( ptr = head ; ptr != NULL ; ptr = temp) { allocated, it has to remember the size of the allocated temp = ptr->next; block (so that it can be subsequently freed). Remember free(ptr); that malloc returns a void *. Since malloc does not know } the type to which the returned block will be cast and used, it has to allocate a block that is within the maximum The implementation for dynamic allocation outlined here alignment requirements imposed by a platform. Typically, uses a straightforward, simple implementation. However, this is 4 to 8 bytes. In addition, since dynamic memory real-world implementations are obviously more complex. For allocation is implemented using (usually) a singly linked list, example, few C implementations provide a user directed Small at least one pointer to the next block needs to be stored. So, Block Allocator (SBA) mechanism so that the programmers can allocation of a block requires at least these fields: tune the C dynamic memory allocator implementation for small l Space to store information about the ‘size’ of the fragment block allocation. In Linux implementations (for example, glibc), l The actual memory of length ‘size’ bytes (within the mmap system call is used to allocate large memory blocks maximum alignment requirements) (segments) with malloc. In this approach, when free is called, l The pointer to the next free block the large blocks (segments) can be returned to the system. So, for example, if the maximum alignment requirement There are lots of resources available on the Web about is 8 bytes, and an integer of 4 bytes is used to store the interesting implementation details—http://en.wikipedia. size of the requested block and the pointer size is 4 bytes org/wiki/Malloc is a good place to start. (which is used to store the next pointer), malloc has to return a block with minimum 16 bytes for a call. In some S.G. Ganesh is a research engineer at Siemens (Corporate other unusual implementation, if the maximum alignment Technology). His latest book is “60 Tips on Object Oriented requirement is 16, and if sizeof int and pointers are 8, it will Programming”, published by Tata McGraw-Hill in December last year. You can reach him at sgganesh@gmail.com. require a minimum of 32 bytes for allocating a single block! 100 march 2008 | LINUX For YoU | www.openITis.com cmyk

×