potential problems with malloc/free vs. C++ new/delete ???




I know many of you are also writing C++ code using gtk so hopefully
you can answer my question.  I remember reading somewhere that it's a
"bad idea" to mix malloc/free with new/delete (if you allocate with
new, you need to deallocate with delete and if you allocate with
malloc you need to deallocate with free).  I've got some code where I
allocate with new the strings for a clist like below:

  gchar **text;

  text = new gchar*[8];
  for (i=0; i<8; i++) {
    text[i] = new char[100];
  }

	// set values of text[i]
  
  gtk_clist_insert(clist, location, text);

When I clear the clist, I assume the code deallocates the space for
the strings (correct?) and that it also would use free (obviously
since it's C code).  So far my program appears to works fine, but is
this going to cause a memory problem that is going to haunt me later?

Should I instead be using malloc and free (or the glib wrappers) for
any allocation for gtk even though my program has to use new and
delete for the C++ class alocations?

Thanks,
Dave
dreed@capital.edu



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]