Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > char * string has to be free() or delete []

Reply
Thread Tools

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

 
 
Richard Herring
Guest
Posts: n/a
 
      08-24-2004
In message <(E-Mail Removed)>, Karl Heinz Buchegger
<(E-Mail Removed)> writes
>zalzon wrote:
>>
>> 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.


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.

--
Richard Herring
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      08-24-2004
On Tue, 24 Aug 2004 04:25:41 GMT, zalzon <(E-Mail Removed)>
wrote:

>On Mon, 23 Aug 2004 23:10:25 -0500, Artie Gold
><(E-Mail Removed)> wrote:
>
>>Any time you _dynamically allocate memory_ it must be freed.

>
>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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      08-24-2004

"zalzon" <(E-Mail Removed)> wrote in message news(E-Mail Removed)...

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
How to delete the space characters from the string jayapal C Programming 4 11-20-2007 03:21 PM
8 bit character string to 16 bit character string Brand Bogard C Programming 8 05-28-2006 05:05 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments