Malloc and Delete?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Person
{
   public:
      Person(char* szNewName)
      {
         // make a copy of the string
         m_szName = _strdup(szNewName);
      };

      ~Person() { delete[] m_szName; };

      void PrintName()
      {
         cout << m_szName << endl;
      };

   private:
      
      char* m_szName; 
};


In the above code the _strdup function calls malloc but then the deconstructor calls delete to free up the memory. Can you do this? Mix and match new/delete and malloc/free?

Also, what do the prefix m_ and the suffix _t (not seen in the code above, just curious) on variable names mean?
Can you do this? Mix and match new/delete and malloc/free?


No.

malloc->free
new->delete
new[]->delete[]

Don't mix and match.

Though really, you could just use std::string and not deal with any of this.


Also, what do the prefix m_ and the suffix _t (not seen in the code above, just curious) on variable names mean?


The m_ prefix is sometimes used to denote a member variable.
The _t suffix is sometimes used to denote a typename (not a variable).
Topic archived. No new replies allowed.