C language Generic Programming Example Tutorial _c language

Source: Internet
Author: User
Tags int size

This article describes the C language of the generic programming method, share for everyone to use for reference. The specific analysis is as follows:

First, generics programming allows you to write fully generalized and reusable algorithms that are as efficient as algorithms designed for a particular data type. In C, this type of generic programming can be achieved through a number of means. Here's a method--void* with no type pointer

Look at one of the following function swaps that implements the exchange of two element contents, taking int as an example:

void swap (int* i1,int* i2) { 
     int temp; 
     temp = *I1; 
     *i1 = *i2; 
     *i2 = temp; 
} 

When you want to swap two char types, you have to rewrite a function that has a parameter type of char, is it possible to use an untyped pointer as an argument? See the following changes:

void swap (void *vp1,void *vp2) { 
    void temp = *VP1; 
    *VP1 = *VP2; 
    *VP2 = temp; 
} 

But this code is wrong, is not pass the compiler. First, a variable cannot be declared as void without type. And you don't know what type of arguments to call this function, and cannot determine a type of declaration. Also, you cannot use * on an untyped pointer because the system does not have information that addresses the size of the object. At compile time, the compiler cannot know the type that passed in the parameter of this function. To implement a generic function, you need to pass the address space size of the object to be exchanged at the call, and use the memcpy () function defined in the header file string.h. The changes are as follows:

void swap (void *vp1,void *vp2,int size) { 
   Char buffer[size];//Note that the GCC compiler here is a
   memcpy (buffer,vp1,size) that allows such a declaration; 
   memcpy (vp1,vp2,size); 
   memcpy (vp2,buffer,size); 
} 

When this function is invoked, it can be invoked as follows (the same applies to other types of x, y):

int x = 27,y = 2; 
Swap (&x,&y,sizeof (int)); 

Here's a function of another function:

int lsearch (int key,int array[],int size) {for
   (int i = 0;i < size; ++i)
         if (array[i] = key) return
              I;
   return-1;
}

This function finds the key element in an array of arrays, returns its index after it is found, and cannot find return-1. For example, you can also implement a generic function:

void* Lsearch (void* key, void *base, int n, int elemsize) {for
  (int i = 0;i < n; ++i) {
    void *elemaddr = (char *) Base+i*elemsize;
    if (memcmp (key, elemaddr, elemsize) = = 0) return
      elemaddr;
  }
  return NULL;
}

Third line of code: casts the first address of an array to a pointer to a char type, and makes Elemaddr point to the first address of the i-1 element of this "generic" array by using the attribute with a char type size of 1 bytes. Because it has been said before, you do not know what type of data you are passing in at this time, the system cannot determine how long an element of this array is and how many bytes it needs to jump to the next element, so cast to a pointer to char, plus the product of the element size information passed in by the parameter and the sum of the cumulative number I, that is, the offset address, You can get the first address of the i-1 element of this array. This allows you to get a pointer to the correct element, regardless of the type of pointer to which the incoming argument is directed, to implement generic programming.

function memcmp () prototype: int memcmp (void *dest,const void *src,int n) to compare the contents of two segments of the address space with N-first addresses of dest and SRC respectively.

This function finds the key element in the array base, finds its address information, and returns NULL if it is not found.

If you use a function pointer, you can implement a generic of its behavior:

void *lsearch (void *key,void *base,int n,int elemsize,int (*CMPFN) (Void*,void*,int)) {for
  (int i = 0;i < n; ++i) {
   void *elemaddr = (char *) base+i*elemsize;
    if (CMPFN (key,elemaddr,elemsize) = = 0) return
      elemaddr;
  }
  return NULL;
}

Then define a function to call:

int intcmp (void* elem1,void* elem2) {
    int* ip1 = elem1;
    int* ip2 = elem2;
    return *ip1-*ip2;
}

See the following call:

int array[] = {1,2,3,4,5,6};
int size = 6;
int number = 3;
int *found = Lsearch (&number,array,size,sizeof (int), intcmp);
if (found = NULL)
     printf ("no\n");
else
     printf ("yes\n");

C language can also achieve a certain generic programming, but this is not safe, the system has only a limited check. You must be more careful when you are programming.

I believe that this article on the C programming for everyone to learn the value of a certain reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.