[Users] dynamic memory
Erik Schnetter
schnetter at cct.lsu.edu
Tue Mar 6 10:43:36 CST 2012
Maria
I see nothing wrong in the code you quote. There is probably a problem
in writing to this array, which leads to the segfault later.
In C++, I often use vectors instead of explicit memory allocation. For
example, if you write
#include <vector>
vector<double> reinarray(n);
then the array will be allocated. It will be freed automatically when
the variable reinarray goes out of scope. In between, you can use
either reinarray[i] to access element, or the more elaborate
reinarray.at(i) which also checks that i is not out of bounds.
-erik
On Tue, Mar 6, 2012 at 11:37 AM, Babiuc-Hamilton, Maria
<babiuc at marshall.edu> wrote:
> Hello,
>
> I have a really hard time in trying to declare a temporary array in C++ using Carpet. The same code used to work just fine with Pugh.
> The error I am getting at run time is:
>
> malloc: *** error for object 0x102811c08: incorrect checksum for freed object - object was probably modified after being freed.
>
> This appears at the simple attempt to add #include <assert.h> or #include <new> in the header of the file.
> The header I am using is:
> #include "cctk.h"
> #include "cctk_Parameters.h"
> #include "cctk_Arguments.h"
>
> //#include <assert.h>
> //#include <new>
>
> #include <cmath>
> #include <cstdlib>
> #include <cstdlib>
> #include <cstdio>
>
> using namespace std;
>
> The way I am trying to define the array is
> with malloc/free
> double *reinarray;
> reinarray = (double*)malloc(n*sizeof(double));
> assert(reinarray);
> .........some code.....
> free(reinarray)
>
>
> with new/delete:
>
> double *reinarray;
> reinarray = new double[n];
> ......some code......
> delete[] reinarray;
>
>
> What I am doing wrong?
>
> thanks,
> Maria
>
>
> _______________________________________________
> Users mailing list
> Users at einsteintoolkit.org
> http://lists.einsteintoolkit.org/mailman/listinfo/users
--
Erik Schnetter <schnetter at cct.lsu.edu>
http://www.perimeterinstitute.ca/personal/eschnetter/
More information about the Users
mailing list