char *Type = new char[10];
Say you have a char array in heap. You want to fill this array with something; say "Access".
If you do;
Type = "Access"
This would be a memory leak!!!!
What happens?
You'll get a warning/error;
The proper way to do this is;
strcpy( Type, "Access" );
But the actual error/warning is because the actual type created by saying Type="Access" is a const char*. The string literal "Access" is usually not inteded to be modifed by something like Type[0] = "a".
Fix would be to do;
const char *Type = "Access";
Say you have a char array in heap. You want to fill this array with something; say "Access".
If you do;
Type = "Access"
This would be a memory leak!!!!
What happens?
You'll get a warning/error;
error: deprecated conversion from string constant to 'char*'
but this is not regarding the memory leak. What actually happens is that the statically allocated string literal will be assigned to the pointer Type, i.e. Type will now point to a different memory and you have no handle to the new'ed memory you actually allocated by new char[10] !!! Ouch! The proper way to do this is;
strcpy( Type, "Access" );
But the actual error/warning is because the actual type created by saying Type="Access" is a const char*. The string literal "Access" is usually not inteded to be modifed by something like Type[0] = "a".
Fix would be to do;
const char *Type = "Access";
Comments
Post a Comment