One common problem that I have noticed is that people copy char* pointers thinking that the pointers will be valid when used but this is rarely the case. See the example below.
//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Example demonstrates allocation of memory for char*
#include<iostream>
using namespace std;
typedef struct {
char const * name;
} TypeInfo;
TypeInfo* MakeType(const char *typeName)
{
TypeInfo* newNamedTypeInfo = new TypeInfo;
char* newTypeName = new char[strlen(typeName) + 1];
strcpy(newTypeName, typeName);
newNamedTypeInfo->name = newTypeName;
return newNamedTypeInfo;
}
int main()
{
TypeInfo* testType = MakeType("Apple Banana");
cout << "testType->name = " << testType->name << endl;
return 0;
}
Please suggest any improvements.
The output is as follows:
Note that there is a memory leak in the program if you can spot it and fix it then you are already half way through :)
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.
ReplyDelete--rha
Yes thats right, I dont release the allocated memory because I use struct and havent defined any destructors.
ReplyDeleteI 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 :)
Hi Zahid,
ReplyDeleteI 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.
Any response is awaited.
Hello Sailesh, good to hear from you.
ReplyDeleteWhat I mean is, if you add the following line before the return in main:
delete testType;
Will you see memory leak?
Yes you will because the name in TypeInfo is not freed. So we can change the TypeInfo as follows:
struct TypeInfo{
~TypeInfo()
{
if(name)
{
delete[] name;
name = NULL;
}
}
char const * name;
};
and memory leak should be gone.
Ps: The above code will look a bit messy because blogger does not allow preformatting in comments
Yaa correct. Thats what I was trying to infer.
ReplyDeleteThanks a lot Zahid :)
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?
ReplyDeletetypedef struct {
char const * name;
} TypeInfo;
TypeInfo* MakeType(const char *typeName)
{
TypeInfo* newNamedTypeInfo = new TypeInfo;
newNamedTypeInfo->name = typeName;
return newNamedTypeInfo;
}
int main()
{
TypeInfo* testType = MakeType("Apple Banana");
cout << "testType->name = " << testType->name << endl;
}
//end main.cpp
Thanks in advance for the clarification.
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.
ReplyDeleteOther problem being that people use reference to copy a local string that becomes invalid as soon as out of scope.