English 中文(简体)
4. 处理堆积层中的gsl矢量
原标题:deallocating gsl vectors in structs

你们能否解释一下我在此所遵循的法典是什么? 我不敢肯定,我是否正确使用过电梯。

With the destructor in there I get:
function1: 23
function2: 8.86183e-317
* glibc detected ./a: double free or corruption (fasttop): 0x000000000111b010 **

If I just comment out the destructor I get:
function1: 23
function2: 24

这是我所希望的。 但是,我不需要花招来避免为更复杂的方案泄露记忆吗?

(因为你可以看到,在点人/一般地点,我可能感到困惑)

感谢!

Edit: oh 是ah,为什么职能1中的额外分配步骤会有所改变?

Edit2:我是否应在构造中将x = 0投入使用? 我认为,这是适当的......在我这样做的时候,我是否应该把它放在初步化上? 因此:x = gsl_vector_alloc(1)。

#include <iostream>
    using namespace std;
#include <cassert>
#include <cmath>
#include <gsl/gsl_vector.h>

struct struct1{
    gsl_vector * x;

    struct1() {
        x = 0;
    }
    ~struct1() {
        if (x) gsl_vector_free(x);
    }
};

void function1(void *p) {
    struct1 s = *(struct1 *) p;
    s.x = gsl_vector_alloc(1);
    gsl_vector_set(s.x, 0, 24);
}

void function2(void *p) {
    struct1 s = *(struct1 *) p;
    gsl_vector_set(s.x, 0, 24);
}

int main() {
    struct1 s;
    s.x = gsl_vector_alloc(1);
    gsl_vector_set(s.x, 0, 23);

    function1(&s);
    cout << "function1: " << gsl_vector_get(s.x, 0) << endl;

    function2(&s);
    cout << "function2: " << gsl_vector_get(s.x, 0) << endl;

    return 0;
}
最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 页: 1 页: 1 这些复印件的号码是x。 每一份复印件的读者,请上gsl_vector_free,因此,你三度用同一点:

  • once in function1 when s is destroyed
  • once in function2 when s is destroyed
  • once in main when s is destroyed

您需要为这一类别安装复印机和复印机。 无论你有哪一类拥有资源,你都需要履行这两项职能,还需要一名司机。 当你使用资源时,需要清理资源。

在你的抽样守则中,最好把所有分配和分配放在该类内,以便你能在不担心的情况下使用该类。 使得这个班子实际管理其资源。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?