answersLogoWhite
Software and Applications (non-game)
Mohandas Gandhi
Free Verse
T-Mobile myTouch

What will happen if you allocate memory using new and free it using free or allocate sing calloc and free it using delete?

333435

Top Answer

User Avatar
Wiki User
2007-12-10 08:31:40
2007-12-10 08:31:40

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.

1

Related Questions

User Avatar

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....

User Avatar

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

User Avatar

|Make yourself conversant calloc, malloc, realloc and free.

User Avatar

Two differences: * new calls the object constructor, whearas calloc() simply initializes the memory to zero. * new is guaranteed to return memory, and throw an exception if it couldn't. calloc() returns NULL if it couldn't allocate memory, and therefore the return value needs to be checked.

User Avatar

int x = 20; // Using calloc/free: char * c = calloc( x, sizeof( char )); // Allocate 20 bytes. free( c ); // Deallocate. // Using malloc/free: char * m = malloc( x * sizeof( char )); // Allocate 20 bytes. free( m ); // Deallocate. // Using new/delete: char * p = new char[ x ]; // Allocate 20 bytes. delete [] p; // Deallocate.

User Avatar

malloc is used to allocate memory for one variable int* hi = (int*)malloc(sizeof(int)); //allocates the amount of memory an integer needs and casts it to an integer (default returns void*) calloc is used to allocate memory for an array int* bye = (int*)calloc(sizeof(int),10); //allocates an array of 10 integers and casts it to an integer pointer you can access calloc'd variables with [] don't forget to free() your allocated variables or you'll have a memory leak nothing will happen on modern operating systems but it's a good programming habit

User Avatar

You access memory with a pointer or a reference to the memory. To allocate memory dynamically, use calloc or malloc (C or C++) or new (C++ only).

User Avatar

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.

User Avatar

AnswerThe use of malloc and calloc is dynamic memory allocation. malloc allocates memory in bytes whereas calloc allocates memory in blocks. calloc initializes the allocated memory to zero. Both differs in number of arguments also. malloc takes only one argument and allocates the memory in bytes as given in the argument. calloc takes two arguments, number of variables to be allocated and size of each variable. Another difference is how the two functions deal with memory alignment.Actually malloc allocates a block of size bytes from memory heap. It allows a programto allocate memory as its needed, and in the exact amount needed . Where as calloc provides access to the C memory heap, which is available for dynamic allocation ofvariable-sized block of memory.

User Avatar

The use of malloc and calloc is dynamic memory allocation. malloc allocates memory in bytes whereas calloc allocates memory in blocks. calloc initializes the allocated memory to zero. Both differs in number of arguments also. malloc takes only one argument and allocates the memory in bytes as given in the argument. calloc takes two arguments, number of variables to be allocated and size of each variable. Another difference is how the two functions deal with memory alignment.Actually malloc allocates a block of size bytes from memory heap. It allows a programto allocate memory as its needed, and in the exact amount needed . Where as calloc provides access to the C memory heap, which is available for dynamic allocation ofvariable-sized block of memory.Read more: Use_of_malloc_and_calloc

User Avatar

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));

User Avatar

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.

User Avatar

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.

User Avatar

yes,it will create memory

User Avatar

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

User Avatar

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.

User Avatar

free() is a function used to free the memory allocated dynamically ,by both malloc and calloc functions. free(ptr): ptr is a pointer to a memory block which has already been creeated by malloc or calloc.

User Avatar

1. First allocate memory for the bigger chunk, e.g. for the array float **matrix, do matrix = malloc(sizeof(n*sizeof(float *)); then for (i=0;i<n;i++) matrix[i] = malloc(sizeof(n*sizeof(float)); You can do calloc for allocation with initializing or, realloc for increase/decrease in size with reallocation.

User Avatar

Delete is a C++ keyword that will de-allocate memory that was allocated using the "new" keyword. If delete is called on a class, the class will execute its destructor function before it is de-allocated. This is useful if your class allocated memory and needs to delete it before it is terminated. Otherwise a memory leak will occur.

User Avatar

The free() function releases memory obtained with the malloc() family of functions. The delete and delete [] operators release memory obtained with the new operator. Note: realloc() can allocate and free memories as well as reallocate.

User Avatar

malloc returns a block of memory that is allocated for the programmer to use, but is uninitialized. The memory is usually initialized by hand if necessary -- either via the memsetfunction, or by one or more assignment statements that dereference the pointer. An alternative is to use the calloc function, which allocates memory and then initializes it. The malloc and calloc differs in the number of arguments. The malloc allocates memory of given size but the calloc can allocates array of memory locations of given size.

User Avatar

None of the data types available in C assigns valur to the variable. Initially all the variables have a garbage value. But when we use calloc() to allocate memory dynamically only then it assigns NULL to the memory block assigned.

User Avatar

calloc is memory allocation function that is normally used for requesting memory space at run time 4 storing data types such as arrays and structures. calloc allocates multiple blocks of storage, each of the same size and then sets all bytes to zero, and then returns a pointer to the memory.


Copyright © 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.