char * string has to be free() or delete []

Z

zalzon

When i create a char * string lets say, do i also have to then
remember to free up the memory after the program is done? Or is it
done automatically?

Is this right?

char * string = "Hello";

string = NULL;
free(string);
 
R

Ron Natalie

zalzon said:
When i create a char * string lets say, do i also have to then
remember to free up the memory after the program is done? Or is it
done automatically?

Is this right?

char * string = "Hello";

string = NULL;
free(string);

No, it is NOT right. char* isn't a string type. It's a pointer to a single char.
It is incumbent on you to remember how it was allocated. In the example
above, it's allocated to a statically allocated string literal. You don't call free
or delete on it.

If you alloc'd it with malloc you'd call free.
If you alloc a single character with new, you free it with delete.
If you alloc an array of char with new [], you free it with delete [].
 
Z

zalzon

You don't free it.


Ah.

What happens to the pointer then?

I'm confused as to when you free and when not to free. I thought
anytime you create a pointer, you have to remember to free it.
 
A

Artie Gold

zalzon said:
Ah.

What happens to the pointer then?

I'm confused as to when you free and when not to free. I thought
anytime you create a pointer, you have to remember to free it.

Any time you _dynamically allocate memory_ it must be freed.

[in your original example nothing is being dynamically allocated]

HTH,
--ag
 
Z

zalzon

Sorry one more question please.

If i malloc() memory to a char * string, do i then do this to free it?

string = NULL;
free(string);
 
K

Karl Heinz Buchegger

zalzon said:
Sorry one more question please.

If i malloc() memory to a char * string, do i then do this to free it?

string = NULL;
free(string);

Yes.

malloc goes with free
calloc free
realloc free

new delete
new[] delete []

In C++ you usually use new/delete and not malloc/free.
You also don't allocate memory to a char* string, but
you use the std::string class.
 
R

Rolf Magnus

zalzon said:
Sorry one more question please.

If i malloc() memory to a char * string, do i then do this to free it?

string = NULL;
free(string);

No. Well, yes, you have to free() it, but not the way you do. What
you're doing is first overwriting the pointer to your allocated memory
with a null pointer, then calling free on that null pointer. The
allocated memory is still existing, and you lost your pointer to it, so
you produced a memory leak. Just leave the "string = NULL;" line out.
 
R

Richard Herring

Karl Heinz Buchegger said:

No. (Those two lines above need to be swapped ;-)
malloc goes with free
calloc free
realloc free

new delete
new[] delete []

In C++ you usually use new/delete and not malloc/free.

And this is the punchline:
You also don't allocate memory to a char* string, but
you use the std::string class.
And for arrays of other types it's usually better to let std::vector
take care of the allocation for you. It's rare to find new[]/delete[]
being used for any good reason outside of library code.
 
T

tom_usenet

So anytime malloc is used, must remember to free.

Right, remember a pointer is just an address, it's what the pointer is
pointing to that needs freeing when it was malloced.

Tom
 
R

Ron Natalie

zalzon said:
What happens to the pointer then?

I'm confused as to when you free and when not to free. I thought
anytime you create a pointer, you have to remember to free it.

You are confused between the concept of a pointer and the value it
contains.

Every object in C++ exists someplace. Sometimes that is in automatically
allocated storage (as a local variable), sometimes statically allocated (a global
or explicit static), sometimes dynamically allocated (new or malloc), some times
it's part of a larger object.

The pointer you used most likely was a auto variable (local to a function). It
takes care of itself.

The question is "What about the value you placed in it?"

This value is a statically allocated character array. It lives for the life of the program.
There's no need to do anything with it.

Had you allocated it with malloc or new, then you'd have to deal with it. However
the fact that it is in the pointer is MEANINGLESS. The pointer is a piece of scratch
paper that you remember the value on. It's HOW the thing you put into it that matters.


Further, at this point, I recommend you get out of the habit of the incorrect notion that
char* is a string type. It is, as I said before, a pointer to a single character. You'll
avoid lots of stupid allocation mistakes if you use the std::string type for strings. This
is C++ after all. The whole char* fiasco is a weak carry over from C.

-Ron
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top