![]() |
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); |
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 []. |
Re: char * string has to be free() or delete []
So how would you free it in the above case then if not with free() ?
|
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. |
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. |
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 |
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. |
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); |
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 |
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.