4
#define BOOST_TEST_MODULE MemoryLeakTest
#include <boost/test/unit_test.hpp>

#include <iostream>
using namespace std;

BOOST_AUTO_TEST_CASE( MemoryLeakTest)
{
    double* n1 = new double(100);
    void* v1 = n1;
    cout << sizeof(v1) << endl;
    delete v1;
}

This code will work perfectly fine without any error leaks. But I would like to be able to get the size of the object void* is holding on to.I would imagine there is a way because the delete statement knew how large the object v1 was pointing to so that it can delete it so it must be stored some where.

Caesar
  • 9,483
  • 8
  • 40
  • 66
  • 6
    Short answer: You can't. You need to keep track of the size yourself. – Mysticial Jul 22 '12 at 05:34
  • 3
    It is stored "somewhere" - and it's completely opaque to you. Deliberately so. If you want to know the size, you need to track it yourself. – paulsm4 Jul 22 '12 at 05:36
  • Applying `delete` to a `void *` pointer has always been illegal in C++. Your code is not even supposed to compile. (Although to my great surprise Comeau Online compiler accepts `delete` on `void *`.) – AnT stands with Russia Jul 22 '12 at 05:47
  • @AndreyT probably has the right answer here; I do get a warning from `clang`. I'm not a C++ expert at all, so I can't directly confirm or rebut. – Carl Norum Jul 22 '12 at 05:57
  • @Caesar - This code doesn't really work according to the language, it is just that *seems to work* is one possible outcome of undefined behavior. – Bo Persson Jul 22 '12 at 11:07

3 Answers3

5

Applying delete to a void * pointer in C++ is illegal.

If your compiler supports this as a non-standard extension, then most likely delete assumes that the unknown object pointed by that pointer has trivial destructor. In that case delete does not have to do anything besides immediately handing over the control to the raw-memory deallocation function ::operator delete, which probably just calls free. (This last bit, of course, can depend on the implementation).

So, your question basically boils down to "how to determine the size of malloc-ed memory block". There's no standard features that can do that. Either memorize the size yourself, when you allocate it. Or use the non-standard implementation-specific library features, if any such features are provided by your platform.

In some implementations this can be done through msize function. But again, in order to do it meaningfully, you'd have to research your implementation first. You need to figure out how the memory is allocated by new and/or what exactly delete v1 does, since it is not standard C++.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

The size of void * will continue to be sizeof a pointer. If you want to find out actual size of the object that the void * is pointing to, you should know the actual object and typecast the pointer to the right type. And no, delete will not know the object pointer size and will result in a leak. The right thing to do, even for delete is to make sure you typecast the pointer to the right type.

Gangadhar
  • 1,893
  • 9
  • 9
  • I just ran the program and I had no leaks. Running 1 test case... 4 *** No errors detected Press any key to continue . . . – Caesar Jul 22 '12 at 05:37
  • Not sure what the test case is in this case. Are you running a test suite to detect memory leaks via valgrind or such? – Gangadhar Jul 22 '12 at 05:38
  • @Gangadhar Using BOOST_AUTO_TEST_CASE which would notify me if it finds a leak. – Caesar Jul 22 '12 at 05:39
  • @CarlNorum, I didn't know that. As mentioned elsewhere on SO http://stackoverflow.com/a/941953/334642, deleting a void* won't call the destructors and can potentially result in a leak. That is what I meant by a leak. – Gangadhar Jul 22 '12 at 05:41
  • @Gangadhar, that's true if you're deleting an object, but in this case the OP has allocated a `double`, which *has* no destructors. – Carl Norum Jul 22 '12 at 05:42
  • @CarlNorum - ack that ! My bad. Should have been a bit more careful when responding. Caesar - there is no memory leak, since you are deleting a basic type. But, it is generally a good practice to ensure you don't delete void* since you might not know the object that the pointer will get pointed to. – Gangadhar Jul 22 '12 at 05:45
  • @CarlNorum That still doesn't answer my question. Let put the destructor aside and just worry about getting the size of the object. – Caesar Jul 22 '12 at 05:48
  • @Caesar, you can't. Several other people have said that too. – Carl Norum Jul 22 '12 at 05:53
0

When doing the cast from double * to void * you are throwing away information for the compiler to work on.

Therefore the compiler does not know what destructos to call or the size of the object.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127