[newbie] question about free() and '\0'

H

Henryk Zabor

This is a silly newbie question.

Here is some (maybe bad) string tokenization code:

/* Tokenise a string */
char *tok = str;
char *p;
for (p=str; *p != '\0'; p++) {
if (*p == ' ') {
*p = '\0';
processToken(tok);
tok = (p+1);
}
}
processToken(tok);

So, a lot of \0 chars are put into the string. Does this hinder the
resource from being freed in the end? Can I still call free() for the
complete string?

I am actually a Java programmer, but have to learn C now. I guess my
biggest problem is how to use free(). When do I have to call free and when
is it not necessary? Where can I find this explained?

Thank you!
Henryk
 
C

Christopher Benson-Manica

Henryk Zabor <[email protected]> spoke thus:

(nitpicks from clc welcome!!)
Does this hinder the
resource from being freed in the end?

It has no effect on whether or not you can invoke free() on the
pointer to the string.
Can I still call free() for the
complete string?

If you allocated it with malloc(), yes. Otherwise, no. Also, you
must not attempt to free() a given pointer more than once.
I guess my
biggest problem is how to use free(). When do I have to call free and when
is it not necessary?

Calls to free() should be in a one-to-one ratio with calls to
malloc(). Un-freed() memory will generally be freed automagically
when the program exits, but not before - so you're better off calling
free() yourself.
Where can I find this explained?

In any of a number of C books - look for dynamic memory allocation.
 
G

gabriel

Henryk said:
So, a lot of \0 chars are put into the string. Does this hinder the
resource from being freed in the end? Can I still call free() for the
complete string?

Look at it this way:

Memory operations are done to blocks of memory, whatever they contain.
So your answer is that free() will be unaffected.

It is only the string functions that have anything to do with zero
characters. By convention (or by specification in the languiage and
library specs), strings are terminated by a zero value. But this is only
a convention, and it does not affect memory allocation.
I am actually a Java programmer, but have to learn C now. I guess my
biggest problem is how to use free(). When do I have to call free and
when is it not necessary? Where can I find this explained?

Simple, always deallocate what you allocate. C does not have any sort of
memory management that deallocates anything you explicitly allocate.

Some operating systems are gracious enough to deallocate all the memory a
process has allocated and not deallocated upon exit, but relying on this
is a bad practice, specially if your system will be running for a long
period of time at a time.

I'd suggest finding a good beginner book on C because you will have
problems down the road if you do not understand memory management in C.
 
D

Dan Pop

In said:
This is a silly newbie question.

Here is some (maybe bad) string tokenization code:

/* Tokenise a string */
char *tok = str;
char *p;
for (p=str; *p != '\0'; p++) {
if (*p == ' ') {
*p = '\0';
processToken(tok);
tok = (p+1);
}
}
processToken(tok);

So, a lot of \0 chars are put into the string. Does this hinder the
resource from being freed in the end? Can I still call free() for the
complete string?

You can call free() ONLY with a pointer value returned by malloc and
friends. It does not matter what happened to the contents of the
allocated memory between the point of allocation and the point of
deallocation (as long as you don't attempt any out of range access).
I am actually a Java programmer, but have to learn C now. I guess my
biggest problem is how to use free(). When do I have to call free and when
is it not necessary? Where can I find this explained?

You have to call free() as soon as you no longer need the allocated memory
block and certainly before you actually lose its address (losing the
address of an allocated memory block is called creating a memory leak,
because the memory block can no longer be deallocated before program
termination).

You don't have to call free() right before program termination, because
it merely burns CPU cycles (free is a relatively complex function).
So, if you have some dynamically allocated data structures that are needed
up to the end of the program, explicitly free'ing them is merely a waste
of CPU resources. Some people prefer to do it, nevertheless, for
religious purposes (they call it a matter of programming discipline :)

Dan
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top