English 中文(简体)
以下是新的超载泄漏记忆吗?
原标题:is the following new overload leaking memory?

我遇到了以下法典:

    class a {
    public:

        void *  operator new(size_t l, int nb);
        double  values;
    };
void *a::operator new (size_t l,int n)
{
    return new char[l+ (n>1 ? n - 1 : 0)*sizeof(double)];
}

从我获得的东西来看,这种结构从“价值”开始:

double* Val = &(p->a->values) + fColumnNumber;

My question is : is there a memory leak? I am very new to overloading new operator, but I m pretty sure that the memory allocated is not deallocated properly. Also does that mean I can never create a "a" class on the stack?

感谢

最佳回答

我认为,它从技术上来说会产生UB,尽管它是一种可能永远不会产生明显副作用的形式(它使用<条码>新[],但我认为,这种编号与“<条码>相匹配,但对于<条码><><>>条/代码”而言,这通常会造成一个明显的问题。

海事组织几乎更糟的是,它利用新的表述来分配实际上应该由tes而不是目标来做的。 如果我这样做的话,我就这样写道:

void *a::operator new (size_t l,int n)
{
    return ::operator new(l+ (n>1 ? n - 1 : 0)*sizeof(double));
}

阁下:

void a::operator delete(void *block)
{
    ::operator delete(block);
}
问题回答

我看不出为什么在<代码>上删除。 * t 无法正确处理这一习俗所分配的记忆<代码> 新的。 检查的最佳途径是实际写出一些法典,并找到,尽管我可能掌握着一种象瓦尔格里德这样的形象。 我假定问询员会发现发生记忆泄漏事件,并怀疑这是原因,因此,围绕这一经营者撰写测试案例似乎是一种值得的尝试。

很显然,如果没有人在随后实际去除,就会泄漏......

I d question the necessity of overriding new for this kind of functionality, but I also assume this was somebody else s code.

很容易发现。 撰写一册电影,建造和修饰a>/em>,观看你的记忆使用。 如果它泄漏,它就会很快。

Its fine as it is, but you d need to use delete[], not delete, from the code that uses this class as it allocates an array. Note that the user wouldn t get any hints that they need to do this - so overloading a delete operator for them would be a good idea.

  1. You can definitely create Class "a" on the stack.
  2. 共有4个新营地(实际上更多,但将遵守基本内容);删除你应当知道的方法签名。

    void* operator new (std::size_t size) throw (std::bad_alloc);
    void* operator new[] (std::size_t size) throw (std::bad_alloc);
    void operator delete (void* ptr) throw ();
    void operator delete[] (void* ptr) throw ();
    

    You are allocating an array within the "operator new" method which should be done in the "operator new[]" method. This will get rid of your nasty check. Write both, "operator new" and "operator new[]"

  3. Don tabes女士希望给打电话者一个“a”类物体(a myA = 新的),以确保你返回“a”而不是果园,因此,你也需要投放。

  4. You need to write the corresponding delete[] and delete methods.

  5. To answer your question, I believe it does leak memory. The new signature you ve provided is called the "placement new". This allows you to allocate a new pointer without allocating memory but to give it a location to point to. Example: If you needed a pointer to a specific point in memory.

    long z = 0x0F9877F80078;
    a myA = new (z) a[5];  // 5 pointers that point to 0x0F9877F80078
    

根据定义,安置新运营商不应分配记忆,因为你已经泄漏。 排除你的第二个论点,因为你有两版新的运营商,你现在可以这样做。 Don tabes 归还物体“a”。

Check out IBM s Info Center: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr318.htm

And the reference or references, cpluplus.com: http://www.cplusplus.com/reference/std/new





相关问题
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?