-
August 11th, 2009, 02:49 AM
#1
Memory free question?
I have the following code
As to my understanding , eventhough I clear the memory at end using free(name), the memory allocated for "First String" still remains in the heap , Am I correct?
Code:
char *name="First String";
//do something
name=strdup("A NEW STR");
printf("%s \n",name);
free(name);
-
August 11th, 2009, 03:08 AM
#2
Re: Memory free question?
it means that you can not use this memory, but this memory does not disappear ,and the data there is not mofiedy and is not necessary to be modifyed.
but it is possible to be modifyed after reallocating this memory and write sth there.
in a word, when u free it, u should not use it.
when u try to access this memory, the behavior is undefined.
-
August 11th, 2009, 03:14 AM
#3
Re: Memory free question?
it means that u do not care for, do not responspible for, do not want to use this memory, while the date int this memory is nono of your business.
-
August 11th, 2009, 03:18 AM
#4
Re: Memory free question?
for instance, you borrow a book from the library, and then you return the book.
now, the book does not disappear, but you can not use it.
-
August 11th, 2009, 03:22 AM
#5
Re: Memory free question?
so ,being a good memory-citizen, i tried to clear that memory before strdump , like :
Code:
char *name="First String";
//do something
//free the frist blob of memory
free(name);
name=strdup("A NEW STR"); //now we have new blob of memort for 'name'
printf("%s \n",name);
free(name);
Now the libC giving the error :
Code:
*** glibc detected *** ./strdup: free(): invalid pointer: 0x000000000040095c ***
======= Backtrace: =========
/lib/libc.so.6[0x7fd94bacd948]
/lib/libc.so.6(cfree+0x76)[0x7fd94bacfa56]
./strdup(__gxx_personality_v0+0x18a)[0x400802]
/lib/libc.so.6(__libc_start_main+0xe6)[0x7fd94ba781a6]
./strdup(__gxx_personality_v0+0x41)[0x4006b9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:05 18809087 /work/RnD/strdup
00600000-00601000 rw-p 00000000 08:05 18809087 /work/RnD/strdup
00cad000-00cce000 rw-p 00cad000 00:00 0 [heap]
7fd94ba5a000-7fd94bba4000 r-xp 00000000 08:05 18997267 /lib/libc-2.7.so
7fd94bba4000-7fd94bda3000 ---p 0014a000 08:05 18997267 /lib/libc-2.7.so
7fd94bda3000-7fd94bda6000 r--p 00149000 08:05 18997267 /lib/libc-2.7.so
7fd94bda6000-7fd94bda8000 rw-p 0014c000 08:05 18997267 /lib/libc-2.7.so
7fd94bda8000-7fd94bdad000 rw-p 7fd94bda8000 00:00 0
7fd94bdad000-7fd94bdc3000 r-xp 00000000 08:05 18997251 /lib/libgcc_s.so.1
7fd94bdc3000-7fd94bfc3000 ---p 00016000 08:05 18997251 /lib/libgcc_s.so.1
7fd94bfc3000-7fd94bfc4000 rw-p 00016000 08:05 18997251 /lib/libgcc_s.so.1
7fd94bfc4000-7fd94c046000 r-xp 00000000 08:05 18997266 /lib/libm-2.7.so
7fd94c046000-7fd94c245000 ---p 00082000 08:05 18997266 /lib/libm-2.7.so
7fd94c245000-7fd94c247000 rw-p 00081000 08:05 18997266 /lib/libm-2.7.so
7fd94c247000-7fd94c338000 r-xp 00000000 08:05 10101066 /usr/lib/libstdc++.so.6.0.10
7fd94c338000-7fd94c537000 ---p 000f1000 08:05 10101066 /usr/lib/libstdc++.so.6.0.10
7fd94c537000-7fd94c53d000 r--p 000f0000 08:05 10101066 /usr/lib/libstdc++.so.6.0.10
7fd94c53d000-7fd94c540000 rw-p 000f6000 08:05 10101066 /usr/lib/libstdc++.so.6.0.10
7fd94c540000-7fd94c553000 rw-p 7fd94c540000 00:00 0
7fd94c553000-7fd94c56f000 r-xp 00000000 08:05 18997270 /lib/ld-2.7.so
7fd94c75d000-7fd94c760000 rw-p 7fd94c75d000 00:00 0
7fd94c76a000-7fd94c76e000 rw-p 7fd94c76a000 00:00 0
7fd94c76e000-7fd94c770000 rw-p 0001b000 08:05 18997270 /lib/ld-2.7.so
7fff5475a000-7fff5476f000 rw-p 7ffffffea000 00:00 0 [stack]
7fff547fe000-7fff547ff000 r-xp 7fff547fe000 00:00 0 [vdso]
fffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
-
August 11th, 2009, 03:24 AM
#6
Re: Memory free question?
Originally Posted by umanga
As to my understanding , eventhough I clear the memory at end using free(name), the memory allocated for "First String" still remains in the heap , Am I correct?
The memory used to store the literal string "First String" could exist anywhere, even in ROM if you are compiling for an embedded platform. I can't say for definite, but I'm pretty sure that the location of the literal string will stay constant and will always be valid.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
August 11th, 2009, 03:37 AM
#7
Re: Memory free question?
Originally Posted by JohnW@Wessex
The memory used to store the literal string "First String" could exist anywhere, even in ROM if you are compiling for an embedded platform. I can't say for definite, but I'm pretty sure that the location of the literal string will stay constant and will always be valid.
the question is not about the storage of string literal or whether you can modify string literal.
the question is what is the result of trying to access a memory that have been freed.
-
August 11th, 2009, 03:46 AM
#8
Re: Memory free question?
Originally Posted by baihacker
the question is not about the storage of string literal
But that's exactly what the question appeared to be about
the memory allocated for "First String" still remains in the heap
i.e. Storage of the string literal.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
August 11th, 2009, 03:47 AM
#9
Re: Memory free question?
Code:
char *name="First String";
//do something
//free the frist blob of memory
free(name);
Don't do that!
The literal string isn't yours to delete.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
August 11th, 2009, 03:49 AM
#10
Re: Memory free question?
Originally Posted by JohnW@Wessex
Code:
char *name="First String";
//do something
//free the frist blob of memory
free(name);
Don't do that!
The literal string isn't yours to delete.
sorry for my mistake
-
August 11th, 2009, 03:50 AM
#11
Re: Memory free question?
Originally Posted by umanga
so ,being a good memory-citizen, i tried to clear that memory before strdump , like :
Code:
char *name="First String";
//do something
//free the frist blob of memory
free(name);
name=strdup("A NEW STR"); //now we have new blob of memort for 'name'
printf("%s \n",name);
free(name);
My program consists of thousands of literal strings. If I had to call free() on all of those strings, I would quit programming in C++.
The free() function is only used on memory gotten by malloc(), calloc(), or realloc(). You did not call any of those functions.
Regards,
Paul McKenzie
-
August 11th, 2009, 04:00 AM
#12
Re: Memory free question?
Originally Posted by Paul McKenzie
My program consists of thousands of literal strings. If I had to call free() on all of those strings, I would quit programming in C++.
The free() function is only used on memory gotten by malloc(), calloc(), or realloc(). You did not call any of those functions.
Regards,
Paul McKenzie
So ,that means the memory allocated for literal strings ,keep untouched in the Heap,until the end of executing the program.Is that so ? If not, whos clearing that memory ?
or the string literals stored in the Stack ? I am confused?
-
August 11th, 2009, 04:04 AM
#13
Re: Memory free question?
Originally Posted by umanga
So ,that means the memory allocated for literal strings ,keep untouched in the Heap,until the end of executing the program.Is that so ? If not, whos clearing that memory ?
It doesn't matter where literal strings are. You are not to "clean them up" explicitly.
Literal strings can be stored anywhere the compiler dictates where they should be stored.
Edit: I didn't see that you were calling "strdup". In that case, you must call free(), since strdup() calls malloc() internally.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 11th, 2009 at 04:08 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|