I am now writing the program code in C ++, using the techniques of C - so familiar. That is, the code is very close to the C program, but compiled by the C ++ compiler. In particular, I allocate memory for structures with functions malloc, free instead of new, delete. Is it right and safe?

// Это C++ struct P { ... }; P *p = (P *)malloc(sizeof(P)); free(p); 
  • You understand that this is just a script. And the normal solution can be described macro and used. Juzat hip - this is nonsense, in general, he is a mistake in NT. There are nuclear services (for example, NtAllocateVirtualMemory) - wrap them in wrappers and use them. - Indy

7 answers 7

Excuse malloc / free from new / delete:

  • new / delete causes destructors, and malloc / free does not;

  • new / delete knows the size of the class (less likely to make a mistake), and malloc / free is not;

  • new / delete can allocate arrays and, when destroyed, call the destructors of each element of the array, but malloc / free does not;

Plus, since there is a c ++ at hand and you need pointers to objects, then why not use the auto_ptr, smart_ptr, weak_ptr idioms in appropriate cases and forget about the hemorrhoids with a bunch of free (p) repetitions, some of which are easy to miss mind

    If OOP is used in the structure, you cannot do this.

    If not, then the situation is as follows:

    • new calculates the size of the structure automatically. malloc needs sizeof, etc.
    • malloc returns void * and you need to do a type conversion. Not nya.
    • new automatically calls the constructor (if any), delete the destructor (similarly, if there is one). malloc / free just operates on chunks of heap memory. Allocates / frees.
    • delete may be overloaded. free - nope Like new / malloc.
    • new throws an exception if the memory is low, and malloc simply returns a null pointer.
    • For arrays, new requires the number of elements, and malloc requires the amount of memory in which you can accidentally make a mistake.
    • However, new / delete does not have such an operator, similar to the realloc function.
    • new is an operator, free is a function =) Well, this is so, for completeness.

    If all this suits you - use =)

      When using malloc there will be serious side effects. You can easily break something inside the implementation of OOP C ++ .. for example, virtual methods may not work. When building different compilers and with different keys, different jambs will come out. So don't even look in the direction of malloc / free :)

      • Is it true or funny? How so ... - Vladimir Gordeev February

      So after all, when malloc () allocates a memory area and the pointer returns, the constructor will not be called. Use C ++ tools and do not interfere with C.

        If you allocate memory for an object, you need to use only new - delete. If it is compatible with C (structures, arrays of variables of built-in types, pointers, etc.), then you can use both.

          And it is also advisable not to forget which pointer was initialized through new, and which one through malloc, in order not to subsequently free the memory allocated by new, through free, and the one through malloc, respectively, through delete (not to mention arrays). And you should not forget that after the memory is freed, the pointers themselves are not deleted, but they remain "hung" in your code, and certainly not for good; advice, not to lose sight of them.

            This is not correct if you are using C ++, because He is focused on data structures. It is correct to write either in C ++ or C. In case you want to use malloc and free, correctly overload the operators New, Delete.

            In general, what does it matter which libraries to use? If you write good code, then there is no difference.

            • <i> In case you want to use malloc and free </ i> <p>!? And who prevents to write: <pre> class my_x {...}; ... void * temp_mem = malloc (sizeof (my_x)); // allocate the memory from where we need it. my_x * my_obj = new (temp_mem) my_x; // use the placing operator new </ pre> True, the destructor will have to be called manually (!?) <i> New, Delete operators. </ i> <p> The idea is not great - there are many pitfalls. - gecube