answersLogoWhite

0


Best Answer

The C++ new uses malloc internally to allocate memory and the C++ delete uses free internally to revoke memory. However, they are not interchangeable and so memory allocated with new MUST be revoked with delete. If you mix them up, you will have a memory leak!

Haya.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What will happen if you allocate memory using new and free it using free or allocate sing calloc and free it using delete?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What if happen when you allocate memory by using malloc or calloc when the momory is not available contigiously?

malloc/calloc/realloc will return NULL


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 are the different function used for dynamic memory allocation?

alloc :- to allocate memory. calloc :- to free the memory.


What is the operator is used to allocate the memory in c plus plus?

calloc operator,malloc operator


What is definition of calloc?

The calloc() Function calloc will allocate space in the memory as well as initialise it to a particular value. Holds 2 arguments, data type and number of datas (n) allocates memory block equivalent to n * data type clears alloted memory with 0 calloc allocates sizeof(datatype) bytes to the no of elements in the file, where by the user can specify the file size as the second arguement. char *calloc(sizeof(datatype), num of elements) calloc() is more efficient as memory is allocated in 1 cycle so fewer clock cycles, more faster executiop.


How can you allocate memory dynamically in c?

char* new_string; // could be any type new_string = (char*) malloc (5120); // allocate memory - typecast is necessary if (new_string == NULL) ... memory exception ... ... use the data ... free (new_string); // release memory when done


Difference beween calloc malloc and realloc?

There are two differences. First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.Here are more opinions and answers from FAQ Farmers:The difference between malloc and calloc are: 1. malloc() allocates byte of memory, whereas calloc()allocates block of memory.Calloc(m, n) is essentially equivalent to p = m*malloc(n); memset(p, 0, m * n); The zero fill is all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values. Free is properly used to free the memory allocated by calloc.Malloc(s); returns a pointer for enough storage for an object of s bytes. Calloc(n,s); returns a pointer for enough contiguous storage for n objects, each of s bytes. The storage is all initialized to zeros.Simply, malloc takes a single argument and allocates bytes of memory as per the argument taken during its invocation. Where as calloc takes two aguments, they are the number of variables to be created and the capacity of each vaiable (i.e. the bytes per variable).This one is false:I think calloc can allocate and initialize memory, if the asked memory is available contiguously where as malloc can allocate even if the memory is not available contiguously but available at different locations.malloc will allocate a block of memoryrealloc will resize a block of memory// allocate a pointer to a block of memory to hold 10 intsint *intArray = malloc(10 * sizeof(int));// change intArray to hold 15 intsintArray = realloc(15 * sizeof(int));


Calloc allocates a block of memory for an array of elements of a certain size?

No. The calloc function allocates a block of memory for a count of a specific type. The size of the type is already known to the compiler so does not need to be specified, it will automatically multiply the type's size by the count. With malloc, you have to allocate memory in bytes, therefore you need to calculate exactly how many bytes you will need for a given type and the number of elements of that type. Examples (allocate 100 integers): int* p = (int*) malloc (sizeof (int) * 100); int* q = (int*) calloc (int, 100); Note also that malloc does not initialise the memory whereas calloc does (the allocated memory is initialised with the value zero). As such, malloc is more efficient when you want to initialise the memory by copying from other memory. That is, there's no point initialising memory you're going to initialise manually, so long as you don't access that memory before it is initialised.


Is it better to use malloc or calloc to allocate memory?

In general using malloc is faster, since calloc initializes the allocated memory to contain all zeroes. If this is what you want, however, then calloc can be used. The results can vary among different operating systems and environments, though. Memory allocation in an OS that uses floating blocks in heaps, such as Microsoft Windows and MacOS, should use the OS-native memory allocators instead. "Use malloc() almost always and calloc() almost never." The reason is that the initialization to zero that calloc() performs is usually not very helpful: - The initialization to "all-bits-zero" is not necessarily the same as initialization to "all-data-zero." C says very little about the representation of values in memory, nothing at all for floating-point or pointer values. On many machines all-bits-zero representations will in fact correspond to f.p. zeroes or null pointers, but this is not guaranteed by the language and there have been machines where the correspondence did not hold. If you get in the habit of using calloc() to initialize f.p. and pointer items, you may be heading for trouble. - Usually, one allocates a chunk of dynamic memory in order to store something in it -- and when you store something in it, you'll overwrite whatever was there before. Thus, the initialization performed by calloc() is usually not needed anyhow. There are occasional exceptions where all- bits-zero initialization is helpful, but they are unusual.


How do you use malloc and calloc to allocate memory for 100 integers?

int *p, *q; p = (int*) malloc (100 * sizeof (int)); q = (int*) calloc (100, sizeof (int)); Note that p is left in an uninitialised state whereas q is initialised with the value zero.


Static and dynamic memory allocation in c plus plus?

dynamic memory allocation is that type of memory which create to allocate the memory on running time or at compile time by the function of malloc , calloc , realloc and free. dynamic memory allocation is give the best utilization of memory which gives the sufficient use of memory.


Which function should be used to free the memory allocated by calloc?

Use the free function to release memory that was previously allocated by malloc, calloc or realloc.