Reputation: 3257
I was wondering the protocol for dealing with memory leaks with pointers to dynamic memory being returned in C and C++. For example, strtok returns a char*. Presumably, the pointer that is returned must eventually be freed/deleted. I note that the reference page doesn't even mention this. Is that because this is simply assumed? Also, how do you know whether to delete or free? Does one have to do research to find out what language each function was originally in, and then assume that all C programs use malloc/free and C++ uses new/delete?
Upvotes: 4
Views: 394
Reputation: 3219
The strtok
function does not perform any memory allocation. It is performing its operations on the string pointer provided by you. By the time the function is called you should have already made your mind about whether to allocate the memory for the pointer on the heap or use automatic stack storage. You could have written:
char * p = "Testing - string";
char * p2 = strtok(p, "- ");
here p2 does not have to bee freed/deleted because it was allocated on the stack. But here
char * p = "Testing - stringh";
char * p2 = malloc(strlen(p));
p2 = strtok(p, "- ");
you have allocated storage for p2 on the heap, so after you have finished with it it has to be freed and set to null:
if(p2 != NULL) {
free(p2);
p2 = NULL;
}
So if you have use new for heap allocation, you then use delete; if you used malloc/calloc, you should the use free.
Upvotes: 1
Reputation: 13532
strtok
is a C function that returns a pointer to a perviously allocated memory; strtok
itself doesn't allocate new memory.
If something is allocated with malloc
it has to be freed with free
; anything allocated with new must be free
d with delete
.
Best way to deal with memory allocation/deallocation in modern C++ is to use smart pointers. Take a look at std::shared_ptr
/std::unique_ptr
and don't use raw pointers unless you absolutely have to.
Also using std::string
and std::vector
will remove most of your raw pointers.
Upvotes: 2
Reputation: 2598
strtok does NOT return a pointer to a newly allocated memory, but rather a pointer to a memory location, which has been previously allocated.
Let's assume this:
char String[1024];
strcpy(String, "This is a string");
char *Ptr = strtok(String, " ");
Ptr will not point to a newly allocated memory location, but rather to the location of String (if my counting doesn't fail me right now), with the space getting replaced by a '\0'. (
From the reference: This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.
That also means, that if you were to print out 'String' again after strtok has done its work, it would only contain 'This', since the string is terminated after that.
As a rule of thumb, you are safe to say, that you only need to free/delete memory you've explicitly allocated yourself.
That means:
For each 'new' put a 'delete', and for each 'malloc' put a 'free' and you're good.
Upvotes: 7