Implementation of free in C++11 using GNU's g++ 4.8.2
Dear all,
I have been constructing a code (for a while now), and for the sake of learning I tried to have this code using features from C++11. I have many, many questions, however, for the sake of being concise I will start with the one in the subject line: If I do: Code:
double* ptr {}; Does the free routine initialize a pointer to nullptr? Thanks, |
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 |
No. Neither free nor delete will set the variable to NULL. If you care about the value you need to set it yourself.
|
Dear a4z:
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: Code:
MEMCHECK_OPTS = -v --tool=memcheck --leak-check=yes I have noticed, that when I do new, instead of malloc or calloc, I get that Valgrind reports errors. Take this code: Code:
#include <iostream> Code:
valgrind -v --tool=memcheck --leak-check=yes ./memtest Code:
... Thanks! |
Dear mina86:
Thanks for your answer! I will keep that in mind. |
You might clear the memory after 'new':
Code:
int n= 100; |
Quote:
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? Thanks! |
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. go there http://msdn.microsoft.com/en-us/library/hh279654.aspx and read. keep an eye on Object Lifetime And Resource Management (Modern C++). search also for othe resources in the internet. good luck. |
> > 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. Just try what I've suggested. Code:
Before: It might. You could simply try it. |
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); |
Wow! Thank you guys for all of your help :) I will try to give an answer to everyone of you :) (It may take some time, but I will)
Let me get myself started! |
Quote:
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++. |
Quote:
You second comment has actually brought me on track to fully adopt new and delete[] :) |
Quote:
Thanks! PS: I will be in Budapest from June, 1st to the 6th attending this event! Let me know if you want to connect! \m/ |
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. |
All times are GMT -5. The time now is 01:33 PM. |