0

私はゆっくりとCでジェネリック関数をプログラムする方法を学び、今や頻繁にトラブルに巻き込まれています。私は2つの配列、この実装では2つのint配列を結合するプログラムを作成しています。最初の問題は、2番目の問題にもつながりますが、compareints(関数)が渡された引数の1つ(void *)にアクセスしないことです。理由がわかりません。ずっと画面を見つめていました...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//makes union of two generic arrays and eliminates duplicates if there are some...
void **
unite(int (*comp)(void *f, void *s), void **first, void **second, int f_size, int s_size,     int bytes, int *x)
{
     int i;
     void **arr=malloc(bytes*(f_size+s_size));
     for(i=0; i<f_size+s_size; i++)
     {
         /* first bigger */
         if(((*comp)(*first, *second))>0)
         {
            *(arr+i)=*(first++);
         }
         /* second bigger */
         else if(((*comp)(*first, *second))<0)
         {
            *(arr+i)=*((second++));
         }
         /* equal => just one copy */
         else
         {
            *(arr+i)=*(first++);
            second++;
         }
     }
     *x=i;
     return arr;
 }

 int
 compareints(void *first, void *second)
 {
     if(*((int *)first)>*((int *)second)) //can't access the memoryloc in second...
         return 1;
     else if(*((int *)first)<*((int *)second))
         return -1;
     else
         return 0;
 }

 int main(int argc, const char * argv[])
 {

     int arr[10]={1, 2, 4, 12, 22, 29, 33, 77, 98};
     int arr2[5]={3, 5, 7, 8, 9};
     void **first=malloc(sizeof(int *)*10);
     void **second=malloc(sizeof(int *)*5);
     //make pointers to static arrays in dynamic arrays
     int f_ind, s_ind;
     for(f_ind=0; f_ind<10; f_ind++)
         first[f_ind]=&arr[f_ind];
     for(s_ind=0; s_ind<5; s_ind++)
         second[s_ind]=&arr2[s_ind];
     int i;
     //make union of the two arrays and print out the result
     void **ret=unite(&compareints, first, second, 10, 5, sizeof(int), &i);
     for(int k=0; k<i; k++)
         printf("%d  ", *((int *)ret[k]));
     return 0;
 }
4

2 に答える 2

1
Why can't function access generic parameter ?

この質問に対する簡単な答えは、関数はアクセスできますが、それ以上の操作void *はできません。

void *渡したアドレスを指しているが、その配列またはメモリ位置の各フィールドのサイズを知らないポインタがあるため、要素はポインタ演算(個々の要素のサイズが必要)を使用してアクセスされます。したがって、アクセスまたは逆参照すると、に移動しUndefined Behaviourます。

関数内のそのタイプの各要素にアクセスする場合はsize、個々の要素のをその関数に渡し、それに基づいて同じタイプへのポインターを作成し、そのタイプの新しいポインターを使用してアクセスします。

詳細についてはこれをお読みください

于 2012-11-29T05:01:48.680 に答える
0

インデックスが範囲外になることについての@WhozCraigsの投稿のおかげでアプローチを試みました。だから私はいくつかの小さな改造を作りました、そして今プログラムはそれが意図することをします。

void **
unite(int (*comp)(void *f, void *s), void **first, void **second, int f_size, int s_size, int bytes, int *x)
{
    int i;
    int f_ind=0, s_ind=0;
    void **arr=malloc(bytes*(f_size+s_size));
    for(i=0; i<f_size+s_size; i++)
    {
        /* first bigger */
        if(((*comp)(*first, *second))>0)
        {
            s_ind++;
            if(s_ind<s_size)
                *(arr+i)=*(second++);
            else
            {
                f_ind++;
                if(f_ind<f_size)
                    *(arr+i)=*(first++);
                else
                    break;
            }
        }
         /* second bigger */
        else if(((*comp)(*first, *second))<0)
        {
            f_ind++;
            if(f_ind<f_size)
                *(arr+i)=*(first++);
            else
            {
                s_ind++;
                if(s_ind<s_size)
                    *(arr+i)=*(second++);
                else
                    break;
            }
        }
        /* equal => just one copy */
        else
        {
            f_ind++;
            s_ind++;
            if(f_ind<f_size && s_ind==s_size)
            {
                *(arr+i)=*(first++);
            }
            else if(f_ind==f_size && s_ind<s_size)
            {
                *(arr+i)=*(second++);
            }
            else
            {
                *(arr+i)=*(first++);
                second++;
            }  
        }
    }
    *x=i;
    return arr;
}
于 2012-11-29T05:58:30.257 に答える