TAILIEUCHUNG - Generic programming

Generic programming Generic programming is about generalizing software components so that they can be easily reused in a wide variety of situations; As a simple example of generic programming, the memcpy function of the C standard library is a generic function to copy data from a container to another. | Generic programming anhtt-fit@ dungct@ Introduction Generic programming is about generalizing software components so that they can be easily reused in a wide variety of situations. As a simple example of generic programming, the memcpy() function of the C standard library is a generic function to copy data from a container to another. void* memcpy(void* region1, const void* region2, size_t n); The memcpy() function is already generalized to some extent by the use of void* so that the function can be used to copy arrays of different kinds of data. Generally, to copy data we need to know only the address and the size of the container to copy. 1 memcpy An implementation of memcpy() might look like the following: void* memcpy(void* region1, const void* region2, size_t n) { const char* first = (const char*) region2; const char* last = ((const char*) region2) + n; char* result = (char*) region1; while (first != last) *result++ = *first++; return result; } Generic functions In a generic function, data should be passed in a generic way (by address and size). If the algorithm demands a specific function to manipulate data (, compare two values), such a function should be passed using a function pointer. Example: A generic search function on an array. How to pass data to this function ? How the algorithm can detect if two data items in the array is equal or not ? 2 Implementation (1) A generic data array should be passed as the following parameters void * buf: the address of the buffer containing the array’s data int size: the size of a data item in the array int total: the total number of data items in the array The search algorithm need also a function to compare the data items in the array for searching. A data item passed to such a function via its address. Use a function pointer to represent a generic comparison algorithm. int (*compare) (void * item1, void * item2) Implementation (2) // return -1 if not found int search( .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.