What if, memory allocated using malloc is deleted using delete rather than free

前端 未结 7 1663
渐次进展
渐次进展 2021-02-09 03:45

I came across an issue which I could not resolve.

My question is, if I used malloc to allocate memory and then memory block is delete using delete

7条回答
  •  北荒
    北荒 (楼主)
    2021-02-09 04:16

    From the documentation,

    void free (void* ptr); 
    

    ptr::

    Pointer to a memory block previously allocated with malloc, calloc or realloc.

    And what you are passing is not a pointer, so it complains.

     A *b = new A();
     cout << b->lol() << endl;
     free(b); // Will Not cause error
    

提交回复
热议问题