Re: Memory contents mysteriously changing
Mark wrote:
Hello, I've run into a strange bug and I'm not sure how to proceed
with fixing it. Any suggestions would be most appreciated.
Here is the relevant code:
template <class T>
class Mesh3d {
public:
// constructors and data access methods (not shown)
Too bad.
write_to_file(string filename);
Probably
void write_to_file(string const& filename) const;
private:
T * data;
A pointer? A *naked* pointer? Why? Couldn't you use 'vector<T>' or
some other container?
}
;
Inside my write_to_file() method, I am invoking a library called SILO
<shrug> You shouldn't assume that we know anything about it.
to write the data to a .silo file, like this:
template <class T>
void Mesh3d<T>::write_to_file(string filename)
{
DBfile * file = NULL;
file = DBCreate(filename.c_str(), DB_CLOBBER, DB_LOCAL, NULL,
DB_PDB); //!!!!
DBPutQuadmesh(file, "SPH_data", NULL, coordinates, dims, ndims,
DB_FLOAT, DB_COLLINEAR, NULL);
DBPutQuadvar1(file, "density", "SPH_data", data, dims, ndims, NULL, 0,
So, here you pass 'data'. Has it been initialized, assigned to
anything? You don't show that part of your code. Memory management can
become tricky. Are you sure you're doing it right?
DB_FLOAT, DB_NODECENT, NULL);
DBClose(file);
}
Note that every function starting with "DB" is a call to the SILO
library. I've double-checked the SILO documentation and as far as I
can tell I am invoking these methods properly (I've also double-
checked that against some sample SILO code, which when on its own
compiles and runs fine).
I invoke the above code from a main() like this:
Mesh3d<float> * mesh = new Mesh3d<float>(xmin, xmax, ymin, ymax, zmin,
zmax, xdim, ydim, zdim);
<shrug> I have no idea what this does to the value of 'mesh->data'.
Next time consider posting more code.
string fn("mesh.silo");
mesh->write_to_file(fn);
There are no compile or runtime errors. But when I watch the contents
of the private variable "data", they mysteriously change. I've
pinpointed the line at which they change, and it is the line I have
commented above with the //!!!! (ie: it's the DBCreate line).
Strangely, the DBCreate function is passed NO information pertaining
to "data", and as far as I can tell it has no pointers to "data" or
any other way in which "data" could be in scope inside the function
DBCreate().
However, when I run this in a debugger, just before calling DBCreate I
have:
(gdb) p data[614]
Why 614?
$1 = 0.904355466
And immediately after the DBCreate line I have:
(gdb) p data[614]
$2 = 4.17010799e-34
What's the significance of '614'?
I just used the index 614 as an example... some entries in the "data"
array change, and some don't. 614 happens to be one of those that
changes.
Example? What's the *real* size of the data, how do you allocate memory
for it, how do you fill the memory up?
How could the data at this memory location be changing?
If you don't designate the memory as belonging to your program, why
can't it change?
> I get no
errors, no seg faults, or anything like that.
It doesn't mean your program is OK. Most likely you have undefined
behaviour due to accessing memory you didn't allocate.
> And the DBCreate()
routine should have nothing to do with the "data" array,
"Should"? And why are you calling it "an array"? It's a pointer. What
it points to is unknown, at least to us (you didn't show us how the
memory is managed).
> as far as I
can tell, and yet according to the debugger it seems to be the line
that is causing the memory contents to change.
Hopefully I am just overlooking something simple, but right now I am
quite baffled by this. Any insights would be very helpful!
Something simple? Well, yes, I suppose. You use a pointer that points
to nothing (or rather who knows where it points to), as if it were an
array of values you're allowed to change. Most likely. If you intended
to dynamically manage your memory, it's fine. Just do it right. Since
my crystal ball is in the shop right now, I can't see inside your
computer's RAM/drive or inside your brain to know how 'data' member in
your class is manipulated. Post more code.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask