0

I am trying a sample program where I allocate memory using "malloc" and de-allocate memory using "free"... I am aware of the fact that using "new" and "delete" is the proper way, however I just want to understand....

I get a crash "segmentation Violation", but I could not understand why...

class Object{
    public:
       Object(){
          this->def = 10;
          std::cout<<"Object Constructed"<<std::endl;
        }
        ~Object(){
             std::cout<<"Object Destructed"<<std::endl;
         }
         void amIPresent(){
              std::cout<<"Yes Object is Present, Defaulter is "<<this->def<<std::endl;
          }
     private:
         int def;
 };


 int main(){
     std::cout<<"Using malloc to Construct Object"<<std::endl;
     Object *o = static_cast<Object*>(malloc(sizeof(Object)));
     Object o2;
     o = &o2;
     std::cout<<"Freeing Memory using \"free\""<<std::endl;
      o->amIPresent();
      free(o);
      return 0;
 }

Output:

 Using malloc to Construct Object
 Object Constructed
 Freeing Memory using "free"
 Yes Object is Present, Defaulter is 10
 Segmentation fault
Sam Daniel
  • 51
  • 4
  • You should never allocate objects with `malloc` that way! Not calling the constructor is only the first part of the problem you will have. If you want to dynamically create a new object, use `new`. – Some programmer dude Apr 19 '12 at 13:00
  • Also, in the future, if you get something like segmentation fault or other crashes again, you should first use a debugger to see what happens and to examine variables. If you stepped over all statements and printed `o` at each step you would have seen the problem quite clearly. – Some programmer dude Apr 19 '12 at 13:02

4 Answers4

4

Because you are reassigning o to point at an object that wasn't created with malloc. So calling free on it makes no sense.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
2

When you free o, it points to the address of o2 which was allocated on the stack (as a variable). You must not use free for stack objects.

0

I think you misunderstand how this code works:

 Object *o = static_cast<Object*>(malloc(sizeof(Object)));
 Object o2;
 o = &o2;

First

 Object *o = static_cast<Object*>(malloc(sizeof(Object)));

will allocate a piece of memory on the heap and the variable o will point to it.

 Object o2;

will create an object on the stack (allocated on the stack, and the constructor is called).

 o = &o2;

will make o point to that object on the stack, it no longer referring to the memory you allocated on the heap using malloc.

So when you use

  free(o);

it will try to free the memory on the stack and not the memory you allocated using malloc, so you crash.

You should look into placement new. See for example What uses are there for "placement new"?

Community
  • 1
  • 1
BertR
  • 1,657
  • 11
  • 12
  • Here is another example of using malloc and placement new: http://stackoverflow.com/questions/7960556/using-placement-new-malloc-and-free – BertR Apr 19 '12 at 13:19
0

You've declared a pointer o which you assign some space on the heap but then you reassign it to point at a stack based object o2 I'm not sure what your logic is in doing that and by doing so you've caused a memory leak.

You then free the memory associated with o2 but then 'o2' is then destructed - as all stack based variables automatically are when you exit their scope - which tries to release memory which has already been freed.

The problem here is that you're mixing stack and heap based objects. Never try to delete (or free) an object created on the stack and, as other have said, in C++ stick to using new and delete exclusively for heap based objects.

Component 10
  • 10,247
  • 7
  • 47
  • 64