Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   char * string has to be free() or delete [] (http://www.velocityreviews.com/forums/t285179-char-string-has-to-be-free-or-delete.html)

zalzon 08-24-2004 12:54 AM

char * string has to be free() or delete []
 
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);

Ron Natalie 08-24-2004 01:13 AM

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

"zalzon" <zalzonishappy@zalll.com> wrote in message news:rs4li05nbopa8hsoiri7cgskjg70h1usep@4ax.com...
> 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 [].


zalzon 08-24-2004 02:45 AM

Re: char * string has to be free() or delete []
 
So how would you free it in the above case then if not with free() ?



Chris Dutton 08-24-2004 03:37 AM

Re: char * string has to be free() or delete []
 
zalzon wrote:
> So how would you free it in the above case then if not with free() ?


You don't free it.

zalzon 08-24-2004 03:57 AM

Re: char * string has to be free() or delete []
 
On Tue, 24 Aug 2004 03:37:41 GMT, Chris Dutton <rubyguru@hotmail.com>
wrote:

>zalzon wrote:
>> So how would you free it in the above case then if not with free() ?

>
>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.

Artie Gold 08-24-2004 04:10 AM

Re: char * string has to be free() or delete []
 
zalzon wrote:
> On Tue, 24 Aug 2004 03:37:41 GMT, Chris Dutton <rubyguru@hotmail.com>
> wrote:
>
>
>>zalzon wrote:
>>
>>>So how would you free it in the above case then if not with free() ?

>>
>>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.


Any time you _dynamically allocate memory_ it must be freed.

[in your original example nothing is being dynamically allocated]

HTH,
--ag
--
Artie Gold -- Austin, Texas

20050120->44

zalzon 08-24-2004 04:25 AM

Re: char * string has to be free() or delete []
 
On Mon, 23 Aug 2004 23:10:25 -0500, Artie Gold
<artiegold@austin.rr.com> wrote:

>Any time you _dynamically allocate memory_ it must be freed.


So anytime malloc is used, must remember to free.

Got ya.



zalzon 08-24-2004 08:29 AM

Re: char * string has to be free() or delete []
 
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);



Karl Heinz Buchegger 08-24-2004 09:17 AM

Re: char * string has to be free() or delete []
 
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.

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.

--
Karl Heinz Buchegger
kbuchegg@gascad.at

Rolf Magnus 08-24-2004 09:56 AM

Re: char * string has to be free() or delete []
 
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);


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.



All times are GMT. The time now is 02:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57