freeing memory in C++

Discussion in 'Mac Programming' started by farmerdoug, May 26, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Using C++, I have to create a vector x using MatDoub several times over as I need to specify the exact value of k each time.
    Below is the output of the search for all the instances of MatDoub in the code.

    Can anybody tell me how to free x?
    thanks


    Code:
    main.cpp:     MatDoub x(k,ndim);
    krig.h: 	const MatDoub &x;
    krig.h: 	MatDoub v;
    krig.h: 	Krig(MatDoub_I &xx, VecDoub_I &yy, T &vargram, const Doub *err=NULL)
    krig.h: 	Powvargram(MatDoub_I &x, VecDoub_I &y, const Doub beta=1.5, const Doub nug=0.)
    interp_2d.h: 	const MatDoub &y;
    interp_2d.h: 	Bilin_interp(VecDoub_I &x1v, VecDoub_I &x2v, MatDoub_I &ym)
    interp_2d.h: 	const MatDoub &y;
    interp_2d.h: 	Poly2D_interp(VecDoub_I &x1v, VecDoub_I &x2v, MatDoub_I &ym,
    interp_2d.h: 	const MatDoub &y;
    interp_2d.h: 	Spline2D_interp(VecDoub_I &x1v, VecDoub_I &x2v, MatDoub_I &ym)
    interp_2d.h: 	const Doub d1, const Doub d2, MatDoub_O &c) {
    interp_2d.h: 	MatDoub c(4,4);
    LUdcmp.h: 	MatDoub lu;
    LUdcmp.h: 	LUdcmp(MatDoub_I &a);
    LUdcmp.h: 	void solve(MatDoub_I &b, MatDoub_O &x);
    LUdcmp.h: 	void solve(MatDoub_I &b, MatDoub_O &x);
    LUdcmp.h: 	void inverse(MatDoub_O &ainv);
    LUdcmp.h: 	MatDoub_I &aref;
    LUdcmp.h: LUdcmp::LUdcmp(MatDoub_I &a) : n(a.nrows()), lu(a), aref(a), indx(n) {
    LUdcmp.h: void LUdcmp::solve(MatDoub_I &b, MatDoub_O &x)
    LUdcmp.h: void LUdcmp::solve(MatDoub_I &b, MatDoub_O &x)
    LUdcmp.h: void LUdcmp::inverse(MatDoub_O &ainv)
    nr3.h: typedef const NRmatrix<Doub> MatDoub_I;
    nr3.h: typedef NRmatrix<Doub> MatDoub, MatDoub_O, MatDoub_IO;
    nr3.h: typedef NRmatrix<Doub> MatDoub, MatDoub_O, MatDoub_IO;
    nr3.h: typedef NRmatrix<Doub> MatDoub, MatDoub_O, MatDoub_IO;
    
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    delete k or delete[] k?

    I'm not sure how you expect someone to answer that with what you have provided.

    Edit: See my later post.
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    I can send you more code but I thought having all the lines with and definitions of MatDoub would be sufficient. In any event
    delete [k,ndim] compiles.
    as does
    delete &y[k] compiles for VecDoub. However both fail when the code runs.

    Can you tell me if any of the h files like it would be helpful.
     
  4. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    Looks like you simply grepped through your code for "MatDoub" [edit: never mind, you explicitly told us you did this]. You mention a MatDoub called "x", which seems to be the first line in your grep output. If that's the case, it looks like it's being created on the stack, not the heap - in that case, you don't need to delete it at all. It will just automatically vanish at the end of its scope.

    This is the most important feature of C++, in my opinion.

    Your example use of delete looks quite dangerous.
     
  5. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    Oops, reading comprehension failure :).

    When I said delete k or delete[] k I meant delete x or delete[] x.
     
  7. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #8
    chown.

    I know we've been down this path before. :)
    I've been working in C but found the answer I needed in the newest version of Numerical Recipes now written in C++.
    Cut, paste, make some errors, post some questions, make it work, move on.
     
  8. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    Farmerdoug, I hope I'm not out of line when I call you the Glenn Beck of the MR:programming forum.

    Not accusing, just asking questions.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    Drive-by programming.
     
  10. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #11
    Jared.
    Don't know why you would think you are out of line but I don't get the comparison either. I'm not thin skinned either.
     

Share This Page