malloc and free pb 
Author Message
 malloc and free pb

/*
Hello!
I have a problem with the following code and more exactly
when I call free(...) after having used malloc(..)
        * if I use the lien (1) it doesn't
        * if I use the line (2) it works
could somebody explain me why an error occured and give
advices to prevent me from having to face this kind of pb
once again.
Thks a lot
Samuel
*/

//the main of my little programm just to test it
int main()
{
        int i=0;
        char *errorString;
        char *errorXML;
        stError error = err_stBuildAnError(i, "errorTest",
"hostTest", getpid(), time(NULL));
        err_ErrorToString(&error, errorString);
        err_ErrorToXML(&error, errorXML);
        printf(errorString);
        printf(errorXML);
        return 0;

Quote:
}

//function used to realize this program and... where I have
a pb with pointers and memory allocation
stError err_stBuildAnError(int errnoCode, char *message,
char *hostname, pid_t pid, time_t time)
{...}

stErrorMsg err_stBuildAnErrorMsg(stError *error)
{...}

void err_Error(int errnoCode, char *message)
{...}

char* err_TimeToString(time_t time, char *timeString)
{
        timeString = (char *) malloc(ERROR_TIME_STRING_LEN);
        strftime(timeString, ERROR_TIME_STRING_LEN,
ERROR_TIME_FORMAT, localtime(&time));
        return timeString;

Quote:
}

void err_FormatAnError(stError *error, char *format, char
*errorFormatted)
{
        char *timeString; // (1)
        //  --> if I use line (1), I have a pb with memory
allocation(error raised and program aborted)

        //char timeString[] = ""; //(2)
        //  --> if I use line (2), it doesn't raise any errors

        timeString = err_TimeToString(error->time, timeString);
        errorFormatted = malloc(FORMATED_ERROR_LEN);
        sprintf(errorFormatted,
                format,
                error->errnoCode, error->message, error->hostname,
error->pid, timeString
        );
        free(timeString);

Quote:
}

inline void err_ErrorToString(stError *error, char
*errorString)
{
        err_FormatAnError(error, ERROR_STRING_FORMAT, errorString);
        free(errorString);

Quote:
}

inline void err_ErrorToXML(stError *error, char *errorXML)
{
        err_FormatAnError(error, ERROR_XML_FORMAT, errorXML);
        free(errorXML);

Quote:
}

* Sent from AltaVista http://www.*-*-*.com/ Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful
--



Mon, 19 Aug 2002 03:00:00 GMT  
 malloc and free pb

Quote:

> /*
> Hello!
> I have a problem with the following code and more exactly
> when I call free(...) after having used malloc(..)
>         * if I use the lien (1) it doesn't
>         * if I use the line (2) it works
> could somebody explain me why an error occured and give
> advices to prevent me from having to face this kind of pb
> once again.
> Thks a lot
> Samuel
> */
>[Snip]

> char* err_TimeToString(time_t time, char *timeString)
> {
>         timeString = (char *) malloc(ERROR_TIME_STRING_LEN);
>         strftime(timeString, ERROR_TIME_STRING_LEN,
> ERROR_TIME_FORMAT, localtime(&time));
>         return timeString;
> }

> void err_FormatAnError(stError *error, char *format, char
> *errorFormatted)
> {
>         char *timeString; // (1)
>         //  --> if I use line (1), I have a pb with memory
> allocation(error raised and program aborted)

>         //char timeString[] = ""; //(2)
>         //  --> if I use line (2), it doesn't raise any errors

>         timeString = err_TimeToString(error->time, timeString);
>         errorFormatted = malloc(FORMATED_ERROR_LEN);
>         sprintf(errorFormatted,
>                 format,
>                 error->errnoCode, error->message, error->hostname,
> error->pid, timeString
>         );
>         free(timeString);
> }

> [More snip]

As a matter of fact, there are a few problems in this little code
snippet.  Line (1) is actually closer to being the correct one.
It is line (2) that I'd expect to cause chaos.  That suggests
you've left something out of your example that may be triggering
part of the problem.

The argument timeString passed to err_TimeToString is not
actually required.  Remember, passing arguments to functions
is done by value, which loosely means the value of timeString
received by the function is a copy of the one passed by the
caller.  That means assignments within the function would
not be seen in the outside world although, in this
case, you are returning the modified value.

You may want the prototype of err_TimeToString to be

char *err_TimeToString(time_t *);  /* note passing a pointer */

and use it accordingly.  Passing a time_t (presumably a struct)
by value is not formally incorrect, but is not a good idea for
various reasons.

You may also want to look at the definitions of your various
macros (eg FORMATED_ERROR_LEN) to ensure lengths being
allocated are adequate and that format strings are valid for
the arguments being passed.
--



Wed, 21 Aug 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. malloc and free pb

2. memory to free or not to free, to malloc or not to malloc ?

3. malloc & free in c#

4. Override malloc,calloc,realloc and free?

5. malloc/free reference implementations

6. malloc, realloc, free questions

7. malloc/free work arrays - time?

8. malloc/free questions

9. Speed of realloc versus (free, malloc, and memcpy).

10. Confused (malloc and free)

11. malloc and free

12. custom malloc()free() to record memory usage?

 

 
Powered by phpBB® Forum Software