Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I allocate memory for class objects using malloc?

Tags:

c++

See the following code:

 #include<iostream>
 #include<stdlib.h>


 using namespace std;

 class ex
 {
     int i;
public:
   ex(int x){
     i=x;
     cout<<"\nconstructor";
   }
   void setval(int x)
   {
       i=x;
   }
   int geti(){return i;}

   ~ex()
   {
     cout<<"\ndestructor";
   }
};

int main()
{
  ex *ob;

  ob=(ex*) malloc(sizeof(ex));

  ob->setval(5);

  cout<<ob->geti();

  delete ob;
 }

I thought the above code would show an error but it compiles successfully and shows the output

5
destructor
Process returned 0 (0x0)   execution time : 0.270 s

My question is:

  1. Can I allocate memory for objects using malloc?

  2. How does malloc allocate memory for the class without a constructor parameter?

  3. Can I use malloc() for allocation and delete for deallocate or new for allocation and free() for deallocation?

  4. How is the destructor called without a constructor?

like image 507
srilakshmikanthanp Avatar asked Dec 10 '19 11:12

srilakshmikanthanp


2 Answers

Can I allocate memory for objects using malloc?

Yes, you can allocate memory with malloc and that memory can later be used to store objects.

However, let's rephrase that a bit:

Can I create class objects using malloc?

No.

Your program has undefined behaviour, because you never created an object.

Instead, use new/delete (or smart pointers) for C++ class objects, or just declare them in the normal manner.

How does malloc allocate memory for the class without a constructor parameter?

Exactly that. It allocates memory, but does not actually construct any ex object in that memory.

You could now use placement new to do so, if you really need to.

Can I use malloc() for allocate and delete for deallocate or new for allocate and free() for deallocate?

No.

How is the destructor called without a constructor?

There is no meaningful way to answer this question, because your program has undefined behaviour.

like image 187
Lightness Races in Orbit Avatar answered Sep 20 '22 18:09

Lightness Races in Orbit


1)Can I allocate memory for objects using malloc

Sure. But malloc doesn't create any objects. Objects with dynamic storage can be created with new-expressions. Placement-new can be used to create an object into memory allocated with malloc.

2)How does malloc allocate memory for the class without a constructer parameter?

There is no difference between allocating memory for one type or another, besides the amount of memory (and potentially alignment of the memory).

3)Can I use malloc() for allocation and delete for deallocate or new for allocation and free() for deallocation?

Only if you don't mind your program having undefined behaviour. And you should mind. Free may be used only with pointers from malloc (or realloc, strdup etc.). delete may be used only with pointers from new. delete[] may be used only with pointers from new[].

4)How is the destructor called without a constructor?

Constructors don't have effect on whether destructor is called. Destructor is invoked when object is destroyed. delete destroys the pointed object. If there is no object, then the behaviour of the program is undefined. This is the case with your program. Behaviour is also undefined because the pointer was not a result of new, but rather malloc. And also because member functions were called through a pointer to a non-existing object.

like image 33
eerorika Avatar answered Sep 19 '22 18:09

eerorika