Hi all,

Why do I get a fatal error when I try and free this memory?

char *timeBegin = (char *)malloc(sizeof(char) * 21);
	timeBegin = time_stamp();
	sleep(1);
	char * test;
	test = calc_time_difference(timeBegin);
	printf("%s", test);
	free(timeBegin);

time_stamp() returns a static char array. Would this be the cause?

>char *timeBegin = (char *)malloc(sizeof(char) * 21);
timeBegin is assigned a pointer to dynamic memory.

>timeBegin = time_stamp();
timeBegin is immediately re-assigned to a different pointer, which is not dynamically allocated. The first pointer returned by malloc is lost forever, thus causing a memory leak.

>free(timeBegin);
You're not allowed to free a pointer unless it was originally returned by malloc, calloc, or realloc because it doesn't exist within the dynamic memory manager's list of allocated blocks.

ah kk. I was trying to assign the value to what time_stamp() returns to timeBegin.
thanks.

How would I return a value and assign it to timeBegin? with out haveing to make the local variable (in time_stamp()) static?

So I see, though you'll need to go about it less directly:

char *temp = time_stamp();
char *timeBegin = malloc(strlen(temp) + 1);

if (timeBegin == NULL) {
    /* Handle the error */
}

strcpy(timeBegin, temp);

Since the string returned by time_stamp is stored in a static array, you need to make a copy of the data before the next call to time_stamp.

[edit]
>with out haveing to make the local variable (in time_stamp()) static?
Now I'm confused. Is it static or not? That makes a huge difference in your program's correctness. Your first post says it's static and now you imply that it's not.
[/edit]

ah k, makes sense. Thanks so much.

edit]
>with out haveing to make the local variable (in time_stamp()) static?
Now I'm confused. Is it static or not? That makes a huge difference in your program's correctness. Your first post says it's static and now you imply that it's not.
[/edit]

Im just trying to figure out the the best way to assign the value. I can change it from static to not static, but I can't figure out how to return a local variable.

code for time_stamp():

/*
 Returns the current time.
*/
char *time_stamp(){
	
	static char str[21] = {0};

        ....//get time

	return str;
}

also how would I initialize str, so it returns a new result every time?

>I can't figure out how to return a local variable.
There are two other common options aside from making the variable static:

  1. Pass in a buffer and work with it.
    char *time_stamp(char *buf, size_t size)
    {
        /* Populate buf */
    
        return buf;
    }
  2. Dynamically allocate the memory.
    char *time_stamp(void)
    {
        char *str = malloc(21);
    
        /* Populate str */
    
        return str;
    }

Just as with static, these options have advantages and disadvantages. It's really up to you to pick which one works best for your application. However, I'll mention that I usually pass in a buffer. That way the caller has more control over memory and things remain cleaner (if more verbose on the caller side).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.