Generic Programming in C

Feb 16, 2010 at 5:59pm
Now I am learning Generic Programming in C.

My homework is writing two function:
- Generic copy function (whose functionality is same as memcpy())
- Generic compare function (which can compare two data of any type such as int, float, double, ... ) (not using template or something which is not exist in C)

Here is my first fuction:
1
2
3
4
5
6
7
8
9
10
void* myMemcpy (void* Destination, const void* Source, int num)
{
	char* first = (char*)Source;
	char* last = (char*)Source + num;
	char* result = (char*)Destination;
	while (first != last)
		*result++ = *first++;
		
	return Destination;
}


But I get a problem with second function. Can you give me some advices?
Thanks for your help.
Feb 16, 2010 at 6:37pm
post the code for your second function, as well as the problem your having with it.
Feb 16, 2010 at 6:41pm
Can we see what you have done?
Feb 16, 2010 at 6:49pm
The second function should look very much like the first, except for line 7, which should compare the two values instead of copy one to the other. If the comparision is 'not equal' then your function should terminate with the appropriate 'not equal' code. Otherwise it will eventually find its way to line 9, where it should terminate with the 'equal' code (which in the case of memcmp() is zero).

By the way, you are casting away the constness of your Source, which is a Bad Thing. Don't do that. Finally, take care that your users don't give you a negative num.
1
2
3
4
5
6
7
8
9
10
void* myMemcpy (void* Destination, const void* Source, int num)
{
	const char* first = (const char*)Source;	// Stays const!
	const char* last = first + num;			// Stays const!
	char* result = (char*)Destination;
	while (first < last)			// No negative num allowed!
		*result++ = *first++;

	return Destination;
}

Hope this helps.

[edit] BTW, your professor should not be calling this "Generic Programming". It is not.
Last edited on Feb 16, 2010 at 6:51pm
Feb 16, 2010 at 7:17pm
A comparison function for equality only would be straight forward - but I can see less-than and greater-than
test being very messy.
Feb 16, 2010 at 7:23pm
Unless I'm reading into the question a bit too far, it almost sounds as if the second one should take variable arguments and then compare the memory they occupy.

EDIT: Ah, nevermind, void* would handle the variable argument part.
Last edited on Feb 16, 2010 at 7:25pm
Feb 17, 2010 at 2:50am
@everyone: thank you very much.
Here is my function after consulting your ideas:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int GenericCompare (const void* ptr1, const void* ptr2, int num)
{
    const char* first = (const char*)ptr2;
    const char* last = (const char*)ptr2 + num;
    const char* result = (const char*)ptr1;
    while (first < last)
        if (result != first)
            return 0;               // not equal
        else
        {
            ++first;
            ++result;
        }
    return 1;                       // equal
}

Topic archived. No new replies allowed.