In What Cases Do I Use Malloc And/Or New

differences between new and malloc in c++ [duplicate]

malloc allocates memory only, it doesn't invoke constructors which can leave objects in an indeterminate state.

In C++ you should almost never use malloc, calloc or free. And if possible avoid new and new[] as well, use object instances or vectors of instances instead.


As for your second question (which is really unrelated to the first), *(myBoxArray2).printer(23) is wrong since the the . selection operator have higher precedence than the dereference operator *. That means first of all that you use the . member selector on a pointer which is invalid, and that you attempt to dereference what printer returns which is also wrong since it doesn't return anything.

You want (*myBoxArray2).printer(23) (note the location of the asterisk is inside the parentheses), which is exactly the same as myBoxArray2->printer(23).

Also note that myBoxArray2->printer(23) is the same as myBoxArray2[0].printer(23).

When to use Malloc instead of New [duplicate]

A couple that spring to mind:

  • When you need code to be portable between C++ and C.
  • When you are allocating memory in a library that may be called from C, and the C code has to free the allocation.

When and why to use malloc?

malloc is used for dynamic memory allocation. As said, it is dynamic allocation which means you allocate the memory at run time. For example when you don't know the amount of memory during compile time.

One example should clear this. Say you know there will be maximum 20 students. So you can create an array with static 20 elements. Your array will be able to hold maximum 20 students. But what if you don't know the number of students? Say the first input is the number of students. It could be 10, 20, 50 or whatever else. Now you will take input n = the number of students at run time and allocate that much memory dynamically using malloc.

This is just one example. There are many situations like this where dynamic allocation is needed.

Have a look at the man page malloc(3).

Does ::operator new(size_t) use malloc()?

The details of how operator new is implemented are property of a particular implementation of standard library - not even a compiler or operation system. I am familiar with one (gnu) and aware of 3 others - CLang, Apache and MSFT. All of them are using malloc() within operator new, because it just makes a life of library developer so much easier.

If malloc() were not used, said developer would have to reimplement a lot in terms of memory allocation, and sprinkle the code heavily with OS-dependent logic to actually request memory. No one wants to do this when malloc() is already there. But by no means they are obliged to use it.

What is the difference between new and malloc and calloc in C++? [duplicate]

new and delete are C++ specific features. They didn't exist in C. malloc is the old school C way to do things. Most of the time, you won't need to use it in C++.

  • malloc allocates uninitialized memory. The allocated memory has to be released with free.
  • calloc is like malloc but initializes the allocated memory with a constant (0). It needs to be freed with free.
  • new initializes the allocated memory by calling the constructor (if it's an object). Memory allocated with new should be released with delete (which in turn calls the destructor). It does not need you to manually specify the size you need and cast it to the appropriate type. Thus, it's more modern and less prone to errors.

When should I use malloc in C and when don't I?

char *some_memory = "Hello World";

is creating a pointer to a string constant. That means the string "Hello World" will be somewhere in the read-only part of the memory and you just have a pointer to it. You can use the string as read-only. You cannot make changes to it. Example:

some_memory[0] = 'h';

Is asking for trouble.

On the other hand

some_memory = (char *)malloc(size_to_allocate);

is allocating a char array ( a variable) and some_memory points to that allocated memory. Now this array is both read and write. You can now do:

some_memory[0] = 'h';

and the array contents change to "hello World"

Using malloc Versus new

malloc is a function call, new in this case an expression.

The difference is; new will allocate memory and construct all the elements of that array with the default constructor. malloc just gives back a chunk of uninitialized memory.

Further still, ::operator new will throw std::bad_alloc or a new handler if one was registered.

The standard library defines a new that accepts a further parameter nothrow which returns a 0 pointer if the allocation fails.

int *x = new(std::nothrow) int[20]; // include <new>, return 0 on failure


Related Topics



Leave a reply



Submit