tag:blogger.com,1999:blog-2349434231090974738.post195720268689672766..comments2023-10-24T19:15:52.177+01:00Comments on Advanced C++ with Examples: Memory Allocation for char*Zahid Ghadialyhttp://www.blogger.com/profile/11618208626682295272noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-2349434231090974738.post-43272838778026888932011-06-07T20:45:36.645+01:002011-06-07T20:45:36.645+01:00Hi Justin, yes you are correct. We have spent coun...Hi Justin, yes you are correct. We have spent countless hours debugging why a log becomes garbage only to find out that this is the way the log string was created.<br /><br />Other problem being that people use reference to copy a local string that becomes invalid as soon as out of scope.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-32868387684453655172011-06-02T19:06:20.962+01:002011-06-02T19:06:20.962+01:00In the introductory comments to this post, you say...In the introductory comments to this post, you say that you have noticed people frequently copying char pointers incorrectly. What is this incorrect way? Is this an example of what you mean?<br /><br /><br />typedef struct {<br /> char const * name;<br />} TypeInfo;<br /><br />TypeInfo* MakeType(const char *typeName)<br />{<br /> TypeInfo* newNamedTypeInfo = new TypeInfo;<br /> newNamedTypeInfo->name = typeName;<br /> return newNamedTypeInfo;<br />}<br /><br />int main()<br />{<br /> TypeInfo* testType = MakeType("Apple Banana");<br /> cout << "testType->name = " << testType->name << endl;<br />}<br /><br />//end main.cpp<br /><br />Thanks in advance for the clarification.Justinnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-49883755573604225392010-11-03T12:07:29.376+00:002010-11-03T12:07:29.376+00:00Yaa correct. Thats what I was trying to infer.
Tha...Yaa correct. Thats what I was trying to infer.<br />Thanks a lot Zahid :)Sailezzzhttps://www.blogger.com/profile/03152608776611493505noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-25653507960779895172010-11-03T11:53:22.166+00:002010-11-03T11:53:22.166+00:00Hello Sailesh, good to hear from you.
What I mean...Hello Sailesh, good to hear from you.<br /><br />What I mean is, if you add the following line before the return in main:<br /><br />delete testType;<br /><br />Will you see memory leak?<br /><br />Yes you will because the name in TypeInfo is not freed. So we can change the TypeInfo as follows:<br /><br />struct TypeInfo{<br /> ~TypeInfo()<br /> {<br /> if(name)<br /> {<br /> delete[] name;<br /> name = NULL;<br /> }<br /> }<br /> char const * name;<br />};<br /><br />and memory leak should be gone.<br /><br />Ps: The above code will look a bit messy because blogger does not allow preformatting in commentsZahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-45265064706213668852010-11-03T11:11:44.354+00:002010-11-03T11:11:44.354+00:00Hi Zahid,
I think memory leak is there only for t...Hi Zahid,<br /><br />I think memory leak is there only for the newTypeName variable not the structure. Your comment above is not clear to me. The structure is any how still pointed to by testType.<br />Any response is awaited.Sailezzzhttps://www.blogger.com/profile/03152608776611493505noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-36065834122588830532010-10-24T07:08:35.813+01:002010-10-24T07:08:35.813+01:00Yes thats right, I dont release the allocated memo...Yes thats right, I dont release the allocated memory because I use struct and havent defined any destructors.<br /><br />I actually meant that if you use this program remember to free the memory. Didnt intend someone to comment on the post and point out the actual problem :)Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-72467531505850829432010-10-23T10:58:52.339+01:002010-10-23T10:58:52.339+01:00The conversion of char* to const char* is valid. S...The conversion of char* to const char* is valid. So I don't see any obstructions there. What you don't do is you don't free allocated memory. Neither pointer to TypeInfo nor pointer to char. But I guess it is not what you wanted to emphasize. Give a clue.<br /><br />--rharhahttps://www.blogger.com/profile/10627501722049904212noreply@blogger.com