[SOLVED] Implementation of free in C++11 using GNU's g++ 4.8.2
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
you do not call malloc / free in C++
use new and delete,
or better, unique_ptr or shared_ptr
feed the search machine of your choice with these keywords
Thanks for your prompt reply. I want to learn. It is my biggest motivation to be alive Which is why I created this example, so that you guys can help me learn, so I can then help others learn
I love Valgrind's memcheck utility. I think it is pretty useful. In fact, in my makefiles, I have:
Note: if you want to set ptr to NULL, do it right after free/delete, not right before malloc/new, that' pointless.
I know that. The reason I do it, is because I have found that if I do not, Valgrind's memcheck complains of the pointer being used without initialization. I do it just to please memcheck.
Now, your suggestion to clear the memory... does it have to do with my post regarding memcheck's error related to the use of new?
ejspeiro, you are mixing C code with C++.
this is technical possible, and sometimes required.
but if you want to learn C++ you should learn modern C++, and not the old style C++ that every one wants to get rid of.
> > If you want to set ptr to NULL, do it right after free/delete, not right before malloc/new, that' pointless.
> I know that. The reason I do it, is because I have found that if I do not, Valgrind's memcheck complains of the pointer being used without initialization. I do it just to please memcheck.
If you allocate an array using new, as in “new double[100]”, you must use delete[] to free the memory.
Also, you should avoid using sizeof with a type. What if you change type of the pointer to be float* and forget to change the type in sizeof? (Worse yet, what if you change to long double*). Instead use sizeof with an expression as an argument, for example:
Code:
ptr = (double *)malloc(10 * sizeof *ptr);
But as it has been mentioned, it's actually even better if you stick to new since it will do what you need, you won't have to cast the return type, and if you change pointer type but forget to change type in new invocation, you'll get compiler error.
ejspeiro, you are mixing C code with C++.
this is technical possible, and sometimes required.
but if you want to learn C++ you should learn modern C++, and not the old style C++ that every one wants to get rid of.
Dear a4z: you are right. In fact, I have taken a good read to the recommended website, and it does look amazing! I will read it profusely!
Just to clarify: The only reason I am doing double* and not something else, is due to the fact that I need to use the Netlib libraries. Basically, due to C++-Fortran interoperability. Granted: there must be a better way to achieve this, using smart pointers, for example! I'll make sure that learning that be a part of my goal regarding learning modern C++.
If you allocate an array using new, as in “new double[100]”, you must use delete[] to free the memory.
Also, you should avoid using sizeof with a type. What if you change type of the pointer to be float* and forget to change the type in sizeof? (Worse yet, what if you change to long double*). Instead use sizeof with an expression as an argument, for example:
Code:
ptr = (double *)malloc(10 * sizeof *ptr);
But as it has been mentioned, it's actually even better if you stick to new since it will do what you need, you won't have to cast the return type, and if you change pointer type but forget to change type in new invocation, you'll get compiler error.
Dear mina86: Nice! Thanks! This did fix my memcheck error! I feel I should apologize, since this is a well-known fact, about C++! Thanks for bringing it up!
You second comment has actually brought me on track to fully adopt new and delete[]
> > If you want to set ptr to NULL, do it right after free/delete, not right before malloc/new, that' pointless.
> I know that. The reason I do it, is because I have found that if I do not, Valgrind's memcheck complains of the pointer being used without initialization. I do it just to please memcheck.
Well, the problem was mixing new [] with delete (instead of delete [])
Clearing the malloc-ed/new-ed memory still might be a good idea, as valgind seems to read your memory-areas, so non-initialized memories might cause false valgrind messages.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.