answersLogoWhite

0


Best Answer

C does not have a new operator, so we must use the malloc function.

In C++ we prefer the new operator over malloc. The new operator not only allocates memory to an object, it invokes that object's constructor, thus ensuring correct initialisation of the object, thus establishing the object's invariant (if it has one). If construction fails for any reason, an exception will be thrown and no object will be instantiated. If the class designer has made correct use of resource acquisition is initialisation(RAII), any resources consumed by the constructor prior to throwing the exception will be automatically returned to the system, thus ensuring no resource leaks occur.

The sole purpose of malloc is to allocate memory and nothing more. If the allocation fails, a null pointer is returned, otherwise the start address of the allocation is returned. However, the memory is left in an uninitialised state even if the object has a constructor. Moreover, neither malloc, calloc nor realloc will throw exceptions so they are unsuitable for enabling RAII. The only reason they exist at all in C++ is simply for the sake of backward compatibility with C code which cannot use the new operator (since it does not exist in C).

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Malloc, because C does not have a new operator.

New is a C++ operator where new is preferred over malloc because new enables RAII (resource acquisition is initialisation) and proper exception handling while malloc simply allocates uninitialised memory.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which is preffered in C malloc or new why?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What keyword is using to create objects?

In C# and Visual Basic.NET the keyword is "new". C doesn't have such an animal, but you generally use the library call to malloc to get new memory.


What is Dynamic reallocation?

This involves allocating and de-allocating memory at run-time. Look into the new and delete operators for C++ or malloc and free for C.


What does malloc return in C and C plus plus?

Address of the allocated area, or NULL.


What is the difference between malloc and new other than syntax?

Click on the link to your right for the answer.Answerboth malloc and new functions are used for dynamic memory allocations and the basic difference is: malloc requires a special "typecasting" when it allocates memory for eg. if the pointer used is the char pointer then after the processor allocates memory then this allocated memory needs to be typecasted to char pointer i.e (char*).but new does not requires any typecasting. Also, free is the keyword used to free the memory while using malloc and delete the keyword to free memory while using new, otherwise this will lead the memory leak. AnswerBesides the basic syntactical difference: malloc is a C function which will allocate the amount of memory you ask and that's it. new is a C++ operator which will allocate memory AND call the constructor of the class for which's object memory is being allocated. Similarily, free is a C function which will free up the memory allocated. but delete is a C++ operator which will free up the allocated memory AND call the destructor of the object.Answermalloc in a function and new is an operator, and its a good programming practice to use an operator instead of functions, because function itself requires some time to be executed whenever that particular function is called. so the main difference is that we use operators instead of malloc because of the TIME CONSTRAINT. Answer1.malloc requires the type casting during decleration , where as new doesn't needed the type casting during decleration 2. when ever we use new for allocating memory along with this it calls the constructor of the class for which object memory is allocated 3. in case of malloc free is the word used to clear the memory, where as delete is the format used in case of new to free the memory after usage 4. malloc is function, where as new is operator..so the time required for execution is less in case of new (it being a operator) as compared to malloc(it being a function) Answer1. malloc is a function call, while new is an operator. This difference is syntactic; behind the scenes, they both perform pretty much the same work to allocate the memory, and operator new also invokes any required constructors. There is a commonplace urban myth that operators are somehow faster in your code than functions; this is not correct, as any operator (except for mathematical operations that correspond directly to a single machine-code instruction) invocation amounts to a function call in any case. 2. malloc can fail, and returns a NULL pointer if memory is exhausted. Operator new never returns a NULL pointer, but indicates failure by throwing an exception instead. There is also a nothrow() version of operator new, which does return NULL on failure.


What is a malloc function in C programming with example?

The malloc() function is part of a class of functions that deal with the allocation of memory on the heap. int *a = malloc (sizeof (int) * 100); /* allocate 100 int's */ if (a == NULL) {...} /* deal with possible malloc failure */ /* use a, either as pointer or as array of 100 ints */ free (a); /* release memory back to the library */

Related questions

Comparison of C' malloc and free functions with c new and delete operators?

Hi, The difference between new and malloc: 1.The New is a operator however malloc is a function 2.New returns the object type and there is no typecasting required. In malloc type casting should be done as it returns a void*. 3. The new operator can be overloaded however there is no over loading in C and hence Malloc can not be overloaded. 4. Operater New asks for the number of objects to be allocated however in malloc it will ask you for the number of bytes to be allocated. 5. The New operater will return you a exception of memory is not available however in malloc it will return u a NULL. 6. New is a concept for dynamically allocation in OOPS(C++) however malloc is used in C. The difference between the delete and free is as follows: 1. delete is a operator and can be overloaded however free is a function and can not be overloaded. With Regards, Shashiraja Shastry


What keyword is using to create objects?

In C# and Visual Basic.NET the keyword is "new". C doesn't have such an animal, but you generally use the library call to malloc to get new memory.


What is the difference between delete and free?

Are you talking about freeing dynamically allocated memory in C/C++? free() is a function that you use to release dynamically (i.e. at run-time) created memory in C, using malloc() or alloc() or such other functions. In the same way, delete() is a function that is used in C++ to release memory created at run-time using the function new(). (Note that you can still use malloc and other C functions in your C++ code, but it is not considered a good programming habit. Moreover, new() is easier to use and more flexible, once you get the hang of it. If this is not what you had in mind, then I do not know if this will be of any help to you. addition: -new is constructor of which delete is destructor so use in pairs always.. similarly use malloc with free.. extra note: - no type cast required for new , whereas malloc, free may require it. - new returns exception whereas malloc returns NULL when memory issue.


What is Dynamic reallocation?

This involves allocating and de-allocating memory at run-time. Look into the new and delete operators for C++ or malloc and free for C.


Max memory allocated by malloc in c?

Memory is allocated by malloc from the heap.... so max mem = size of heap that is free...


What does malloc return in C and C plus plus?

Address of the allocated area, or NULL.


What is library function in C programming?

printf, fgets, strlen, malloc etc


What are new and delete operators in c plus plus?

New and Delete are the memory management operators in c++,like c language we use malloc() and calloc() functions to allocate memory and free() functiong to release the memory similarily we use new to allocate memory in C++ and Delete to release the allocated memory....


What is the difference between malloc and new other than syntax?

Click on the link to your right for the answer.Answerboth malloc and new functions are used for dynamic memory allocations and the basic difference is: malloc requires a special "typecasting" when it allocates memory for eg. if the pointer used is the char pointer then after the processor allocates memory then this allocated memory needs to be typecasted to char pointer i.e (char*).but new does not requires any typecasting. Also, free is the keyword used to free the memory while using malloc and delete the keyword to free memory while using new, otherwise this will lead the memory leak. AnswerBesides the basic syntactical difference: malloc is a C function which will allocate the amount of memory you ask and that's it. new is a C++ operator which will allocate memory AND call the constructor of the class for which's object memory is being allocated. Similarily, free is a C function which will free up the memory allocated. but delete is a C++ operator which will free up the allocated memory AND call the destructor of the object.Answermalloc in a function and new is an operator, and its a good programming practice to use an operator instead of functions, because function itself requires some time to be executed whenever that particular function is called. so the main difference is that we use operators instead of malloc because of the TIME CONSTRAINT. Answer1.malloc requires the type casting during decleration , where as new doesn't needed the type casting during decleration 2. when ever we use new for allocating memory along with this it calls the constructor of the class for which object memory is allocated 3. in case of malloc free is the word used to clear the memory, where as delete is the format used in case of new to free the memory after usage 4. malloc is function, where as new is operator..so the time required for execution is less in case of new (it being a operator) as compared to malloc(it being a function) Answer1. malloc is a function call, while new is an operator. This difference is syntactic; behind the scenes, they both perform pretty much the same work to allocate the memory, and operator new also invokes any required constructors. There is a commonplace urban myth that operators are somehow faster in your code than functions; this is not correct, as any operator (except for mathematical operations that correspond directly to a single machine-code instruction) invocation amounts to a function call in any case. 2. malloc can fail, and returns a NULL pointer if memory is exhausted. Operator new never returns a NULL pointer, but indicates failure by throwing an exception instead. There is also a nothrow() version of operator new, which does return NULL on failure.


How is memory management achieved by pointers in c plus plus?

Memory management in C++ is achieved using the new and delete operators. These operators are synonymous with the malloc() and free() functions found in C, and which you can also use in C++ but then your code would not strictly be C++. However, the new and delete operators are implemented behind the scenes with malloc() and free(), so the distinction at this level is somewhat moot.Although the new operator and malloc() function effectively do the same job, the new operator greatly simplifies the process. Whereas malloc() requires that you specify the exact amount of memory required, the newoperator can determine the amount at compile time, based upon the type of memory required. Moreover, the new operator returns a pointer to the required type whereas the pointer returned by malloc() must be cast to the appropriate type.There's very little difference between the deleteoperator and the free() function, however they are not interchangeable. If memory was allocated with the newoperator, then it must be released with the delete operator, not the free() function.Apart from these differences, memory management in C++ is much the same as it was with C. Whether memory is allocated with the new operator or the malloc() function, you must maintain a pointer variable to hold the reference that is returned (or a NULL value if the allocation failed). When the memory is no longer required, it must be released back to the system by deleting or freeing the original pointer or a copy of the original pointer. At that point, all pointers to that memory are deemed invalid, and should be zeroed to ensure they no longer refer to invalid memory.Note that the malloc() function also has two variants: calloc() and realloc(). calloc() works much the same as malloc() but is used to allocate a count of contiguous memory blocks, like an array, while realloc() is used to release an existing allocation and allocate another to the same pointer. There is no equivalent operator for realloc()in C++.


Which Program that will run in C but not in C plus plus?

void main() { int *x = malloc(sizeof(int) * 10); }


What is a malloc function in C programming with example?

The malloc() function is part of a class of functions that deal with the allocation of memory on the heap. int *a = malloc (sizeof (int) * 100); /* allocate 100 int's */ if (a == NULL) {...} /* deal with possible malloc failure */ /* use a, either as pointer or as array of 100 ints */ free (a); /* release memory back to the library */