-
free() or delete?
Can they be used interchangibly (they seem to be able to), and if so, which is better to use (i.e. does every OS/compiler support them, does one have more functionality)?
And continuing that line of thought, is there a better way than using new? I know there are malloc and things like that... but those are probably not as good to use, correct?
Thanks a bunch!
-
Delete usually does the same as free, except that it also calls destructors....so if you call malloc and then delete, you may end up with a crash
if you use new, use delete. If malloc, then free....
In C++ try stick with new/delete
-
you're not supposed to interchange them, new goes with delete and free goes with malloc
new and delete call the constructor and destructor respectively, malloc and free dont
-
-
C++ introduced new and delete to replace malloc() and free(). So when using C++ just use new and delete, but in C these two are not avilable, so just use malloc() and free() when creating a C program.
-
the problem is that I was told to use
strdup()
should I manually allocate it?
-
Then use free(). Note that strdup is actually a very common extention, it's not really true C/C++. It's no sin to use malloc, and particularly realloc in C++, if you know what you are doing. In general, you should avoid the whole issue alltogether and use std::string and std::vector and let them handle all such nasty details as cleaning up after themselves.
-
unfortunately, doing that didn't work. I tried.