K
Karthik D
Hello All,
Basically I would like to take suggestions from programmers
about implementation of a function which processes some error
messages.(fyi:this is not a homework problem)
Let me explain my problem in detail.I have a function
which closely emulates dlerror() function.
The function is as below:
void dlerror(void *handle, char *err, size_t len)
{
char *msg;
/*
* I am using some API(Win32)which converts the error number
* to message and stores it in 'msg'.The memory for msg is
* allocated by the API.So it needs to be freed before exiting
* this function.
*/
/*
* So I try to copy msg into err using memcpy since I need
* to have the error message in calling function
*/
if (msg != NULL)
{
memcpy(err,msg,len);
}
/* freeing the memory allocated for msg */
return;
}
The function is called as follows :
int main()
{
void *hdl;
char err[256];
/*
* ....
*/
dlerror(hdl, err, sizeof(err));
if (err != NULL)
{
printf("Error message is %s\n", err);
}
else
printf("Error message is NULL\n");
return 0;
}
Is this a correct way?If I am wrong or if there are any better/good ways
could someone kindly suggest me how can I go about.
I am finding difficulty in understanding
- the memory manangement of char pointers/double char pointers
inside a function and returning their values to another function
etc..
I have read C books.But if someone could provide some real-world
insights on this,it would be helpful for me.
Thanks & Regards,
Karthik
Basically I would like to take suggestions from programmers
about implementation of a function which processes some error
messages.(fyi:this is not a homework problem)
Let me explain my problem in detail.I have a function
which closely emulates dlerror() function.
The function is as below:
void dlerror(void *handle, char *err, size_t len)
{
char *msg;
/*
* I am using some API(Win32)which converts the error number
* to message and stores it in 'msg'.The memory for msg is
* allocated by the API.So it needs to be freed before exiting
* this function.
*/
/*
* So I try to copy msg into err using memcpy since I need
* to have the error message in calling function
*/
if (msg != NULL)
{
memcpy(err,msg,len);
}
/* freeing the memory allocated for msg */
return;
}
The function is called as follows :
int main()
{
void *hdl;
char err[256];
/*
* ....
*/
dlerror(hdl, err, sizeof(err));
if (err != NULL)
{
printf("Error message is %s\n", err);
}
else
printf("Error message is NULL\n");
return 0;
}
Is this a correct way?If I am wrong or if there are any better/good ways
could someone kindly suggest me how can I go about.
I am finding difficulty in understanding
- the memory manangement of char pointers/double char pointers
inside a function and returning their values to another function
etc..
I have read C books.But if someone could provide some real-world
insights on this,it would be helpful for me.
Thanks & Regards,
Karthik