|
From: Susan M. <sma...@uc...> - 2008-03-17 21:26:42
|
Hello, everyone! I have a question. I have a C++ program which calls into LAPACK for certain linear operations. I had been doing something like:
double *array = new double[my_array_size];
some_lapack_call(array,....);
delete array;
When I ran valgrind, I was getting new/free mismatch errors. So I assumed that because LAPACK was written in C, perhaps the LAPACK call was doing something malloc/free-ish behind the scenes. So I changed my code to
double *array(NULL);
if ((array = (double *) malloc(my_array_size*sizeof(double))) == NULL) {
...out of memory
}
some_lapack_call(array,....);
free(array);
Then all my valgrind errors went away, no more new/free mismatch errors. I am just curious if this was a legitimate error, that LAPACK as a C library should only have blocks of memory passed to it that have been malloc'd, or if this was sort of an anomolous error that I could have ignored.
I would love to hear thoughts and perspectives!
Thanks,
Susan
|
|
From: Dan K. <da...@ke...> - 2008-03-17 21:28:17
|
On Mon, Mar 17, 2008 at 2:26 PM, Susan Margulies <sma...@uc...> wrote: > Hello, everyone! I have a question. I have a C++ program which calls into > LAPACK for certain linear operations. I had been doing something like: > > double *array = new double[my_array_size]; > > some_lapack_call(array,....); > > delete array; > > When I ran valgrind, I was getting new/free mismatch errors. Perhaps because you should have written delete[] array; ? |
|
From: Julian S. <js...@ac...> - 2008-03-17 21:41:20
|
> double *array = new double[my_array_size]; > > some_lapack_call(array,....); > > delete array; I don't know much C++, but I had the impression that if you allocate with T* v = new T [] (for some type T) then you need to deallocate with delete [] v Certainly that's what the relevant regression test, memcheck/tests/mismatches.cpp seems to suggest. So you need "delete [] array;" in your code. J |
|
From: Susan M. <sma...@uc...> - 2008-03-17 22:10:09
|
You are totally right! I am so used to only new-ing objects that I forgot about new[]-ing arrays! I am so sorry to have bothered you!! Best, Susan ----- Original Message ----- From: "Julian Seward" <js...@ac...> To: "Susan Margulies" <sma...@uc...>; <val...@li...> Sent: Monday, March 17, 2008 2:37 PM Subject: Re: [Valgrind-users] new/free mismatch question? > >> double *array = new double[my_array_size]; >> >> some_lapack_call(array,....); >> >> delete array; > > I don't know much C++, but I had the impression that if you > allocate with > > T* v = new T [] > > (for some type T) then you need to deallocate with > > delete [] v > > Certainly that's what the relevant regression test, > memcheck/tests/mismatches.cpp seems to suggest. So you need > "delete [] array;" in your code. > > J |
|
From: Bart V. A. <bar...@gm...> - 2008-03-18 07:10:59
|
On Mon, Mar 17, 2008 at 11:10 PM, Susan Margulies <sma...@uc...> wrote: > You are totally right! I am so used to only new-ing objects that I forgot > about new[]-ing arrays! I am so sorry to have bothered you!! > > Best, > Susan Hello Susan, new[] and delete[] are obsolete C++ features -- it is better to use the class std::vector<> instead. Bart. |