How to define return string in DLL?

H

huey_jiang

Hi All,

I need to make DLL for my new hash function in C. In myhashdll.c file,
I have a function as:

char *make_hash(char *in, char *out)
{
......
......
return (out);
}

So, this function is supposed to return a hashed string in the *out
argument. What I don't know is how to define its header file. Say, in
myhashdll.h file, I need to define 2 values that tell my main() whether
this make_hash() call works good or not. If do defines as:

#define HASH_OK 0
#define HASH_ERR 44

I think these 2 defines should not work right, because my return will
be a hashed sring, instead of 2 numbers. Can anybody help me to figure
out how to do these 2 defines? I mean, to make the defines that can
return my hashed string.

Sorry for the complexity of the question. Thanks in advance!

Huey
 
B

bjrnove

One thing. Why do you return a char* if 0 is going to be ok? If I was
going to write such a function I would make a couple of changes. First
of all I would make the definition something like this:

int make_hash(char* in, size_t insize, char* out, size_t outsize);

This makes it possible to make a hash of something that's not \0
terminated. I would also alow me to return different errorcodes for
different errors. The outsize I would use as a maximum size for the
output. This make the caller responsible for allocating the memory and
I usaly find that more safe for memory leaks. Another thing about the
outsize is that with a hash youknow the size. It will always be the
same and therefor you are able to hash define the size.

So basicly, theres seems to be no good reason for you to return a
char*, and second of all, why return NULL on success?
 
C

CBFalconer

bjrnove said:
One thing. Why do you return a char* if 0 is going to be ok? If I was
going to write such a function I would make a couple of changes. First
of all I would make the definition something like this:

int make_hash(char* in, size_t insize, char* out, size_t outsize);

This makes it possible to make a hash of something that's not \0
terminated. I would also alow me to return different errorcodes for
different errors. The outsize I would use as a maximum size for the
output. This make the caller responsible for allocating the memory and
I usaly find that more safe for memory leaks. Another thing about the
outsize is that with a hash youknow the size. It will always be the
same and therefor you are able to hash define the size.

Keep things simple. The point of a hash is to return an integer of
some size, nothing more (apart from the distribution). So there is
no need to allocate any memory anywhere. The most common use of a
hash is on nul terminated strings, so supplying a length parameter
means prescanning the string with strlen, and will be a significant
unneeded slowdown. What errors (apart from hardware) can occur? A
properly designed hash will have no integer overflows, etc. so an
error return is pointless. Even if I am wrong, some hash
algorithms cannot return zero (such as a CRC with proper
initialization) so a zero error return is perfectly feasible.

You can find some hash functions and tests on them in:

<http://cbfalconer.home.att/download/hshftst.zip>

which you can easily modify to include further functions. It uses
the instrumentation in hashlib to measure the efficacy of the hash
on the data.

DLLs don't enter into it. They are meaningless to the C language,
and thus OT here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top