ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What are the differences between malloc , relloc and calloc functions , are there any similar functions you know at java or c# ?

user-image
تم إضافة السؤال من قبل مستخدم محذوف‎
تاريخ النشر: 2015/08/09
Gayasuddin Mohammed
من قبل Gayasuddin Mohammed , Advocate , Practicing Law before High Court at Hyderabad

malloc, relloc, calloc are the memory allocation functions used in C programming language.

malloc function will just allocate the memory  , relloc is the memory allocation function used for freeing the existing memory and reallocate according to the requirement(increasing or decreasing the blocks)

calloc used for memory allocation and also fills the allocated memory with null values or value can be provided as a parameter.

In C++, new operator is there for allocation of memory.

I think in java or C# programming language have got capability of built-in dynamic memory allocation, no need to allocate memory in programming.

Mohammad Basheer abu ahmad
من قبل Mohammad Basheer abu ahmad , مشرف كمبيوتر ، الإشراف على الموظفين , قطاع عام

Sorry I'm not professional in Java language,Thank you for invitation

Ibrahim Magdy
من قبل Ibrahim Magdy , Senior Full Stack Software Developer , Trustwave

well as been said malloc allocates a memory in the heap it doesn't initialize the memory at all just initialize the size_t value calloc same as malloc but initialize the memory block with zeroes realloc , reallocates a memory block, it usually used in case of expanding shrinking a memory block all of these functions are rarely used in the C++ , yet it is the only way you allocate memory in C, to free memory in heap use free() function alloca() , _malloca() are functions that dynamically allocates memory on the stack generally the stack is allocated by compilers, so unless you really know what you are doing you shouldn't allocate in stack in C++ it is more frequent and convinient that people use new and delete instead, new allocates the memory and call the constructor of the class "in case it is a class", delete calls destructor then free memory regarding the c# and Java these are different languages that uses interpreters, JIT and "usually" a garbage collector Each interpreter has his own way of garbage collection, a garbage collector screens memory freeing from code, the interpreter screens the allocation along with type definitions in case of c# since it is "usually" static compile , basically a garbage collector generate extra memory block beside any object that have a reference count and other things. when it finds out that there is no reference it prepares it for collection, garbage collector do other staff like moving objects allocates memory, alligning object .. etc. It depends on runtime engine how this is done. the reference count is old staff that where also used ATL these are the basics there is a lot of details

Emad Mohammed said abdalla
من قبل Emad Mohammed said abdalla , ERP & IT Software, operation general manager . , AL DOHA Company

ptr =(char**) malloc (MAXELEMS *sizeof(char*));

or:

ptr =(char**) calloc (MAXELEMS,sizeof(char*));

When is it a good idea to use calloc over malloc or vice versa?

 

c malloc calloc

Zeroing out the memory may take a little time, so you probably want to use malloc() if that performance is an issue. If initializing the memory is more important, use calloc(). For example, calloc() might save you a call to memset().

Why would one use realloc() function to resize an dynamically allocated array rather than using free() function before calling the malloc() function again (i.e. pros and cons, advantages vs. disadvantages, etc.)? It's for C programming, but I can't find the proper tag for it. Thanks in advance.

 

The advantage is that realloc will preserve the contents of the memory. With free + malloc you'd need to reset the data in the array.

Rehman Maqsood Maqsood ahmed
من قبل Rehman Maqsood Maqsood ahmed , super viser , tariq construction

 

c malloc calloc

Zeroing out the memory may take a little time, so you probably want to use malloc() if that performance is an issue. If initializing the memory is more important, use calloc(). For example, calloc() might save you a call to memset().

Why would one use realloc() function to resize an dynamically allocated array rather than using free() function before calling the malloc() function again (i.e. pros and cons, advantages vs. disadvantages, etc.)? It's for C programming, but I can't find the proper tag for it. Thanks in advance.

Basith Basheer
من قبل Basith Basheer , Senior Web and Mobile Application Developer , Ministry Of Interior - Kuwait

These functions use in the C language not in c#

Anurag Misra
من قبل Anurag Misra , Senior Consultant , Grant Thornton Bharat LLP

In short basic difference is that

malloc is used to allocate memory.

calloc is allocating memory as well as initializing it.

realloc is used to make change in already allocated memory without loosing stored info.

Normally C# doesn't need such things .Net has its own memory management and only new operator is needed to manage memory. But in very special cases you can you Marshal class to manage memory like Marshal.AllocHGlobal[^] and Marshal.FreeHGlobal[^]

Mohamed Ashraf Pulivetti
من قبل Mohamed Ashraf Pulivetti , IT Consultant , Al Rifaa Information Technology

1 MALLOC()void *malloc(size_t size);size_t corresponds to the integral data type returned by the language operator sizeof and is used to represent the size (in bytes) of an object. It is defined (In string.h header in C language andheader in C++) as an unsigned integral type. It is just an indication that the type is used to hold number of memory bytes (and not usual unsigned int).2.CALLOC() Calloc also allocates memory on heap like malloc does. The only difference is that calloc also initialize the memory with zero (malloc returns uninitialized memory).3. REALLOC()Realloc (change the size of memory block on heap)Suppose a pointer (say ptr) is pointing to a memory of int on heap allocated using malloc as below.    int * ptr = (int*)malloc(*sizeof(int));You want to increase the size of memory pointed to by ptr from to, without loosing the contents of already allocated memory. In this case you can call the realloc function.

Jinu mathukutty
من قبل Jinu mathukutty , Software Engineer , Inplass İnformation Technology LLC

These3 functions are used in C for dynamically  allocating memory.malloc () function is used to allocate space in memory during the execution of the program.It does not initialize the memory allocated during execution.  It carries garbage value.malloc() function returns null pointer if it couldn't able to allocate the requested memory 

calloc () function is also like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc() doesn’t

 

realloc() modify the allocated memory size by malloc() and calloc() new size

If enough space doesn’t exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block

 

except for the stack alloc operator,c# provides no predefined constructs for managing non-garbage collected memory

Sonali Kalla
من قبل Sonali Kalla , Junior Engineer , HALO Engineering Services

malloc initaillize memory of int,and calloc does the same but it initilaize memory with zero..and relloc also initialize memory with zero in array element

Paul McMahon
من قبل Paul McMahon , Tutor , University of Wollongong

Both malloc, realloc and calloc are used in C/C++ language for dynamic memory allocation where they obtain blocks of memory dynamically. Realloc is used to resize the memory allocated earlier by function malloc () to a new size given by the second parameter of the malloc function. 

المزيد من الأسئلة المماثلة