Both the malloc () and the Calloc () functions is used to allocate dynamic memory. Each of the operates slightly different from the other.
Both the malloc () and the Calloc () functions is used to allocate dynamic memory. Each of the operates slightly different from the other. malloc () takes a size and returns a pointer to a chunk of memory in least that big:
void *malloc (size_t size);
Calloc () takes a number of elements, and the size of each, and returns a pointer to a chunk of memory
At least big enough-to-hold them all:
void *calloc (size_t numelements, size_t sizeofelement);
There is one major difference and one minor difference between the functions. The major difference is that malloc () doesn ' t initialize the allocated memory. The first time malloc () gives you a particular chunk of memory, the memory might is full of zeros. If memory has been allocated, freed, and reallocated, it probably have whatever junk was left in it.
*****
That's means, unfortunately, that's a program might run on simple cases (when memory was never reallocated) but broke when used Harder (and when memory is reused).
*****
The meaning of this sentence to see 2 times or mysteryof ....
Calloc () fills the allocated memory with all zero bits. That means this anything there you be going to use as a char or an int of any length, signed or unsigned, is guaranteed t o be zero. Anything going to use as a pointer is the set to all zero bits.
That's usually a null pointer, but it's not guaranteed. Anything you be going to use as a float or double be set to all zero bits; That's a floating-point zero on some types of machines, and not on all.
The minor difference between the and that's calloc () returns an array of objects; malloc () returns one object. Some people use Calloc () to make clear this they want an array.
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/missuever/archive/2005/11/30/540045.aspx
Another explanation:
1. Allocating memory space functions malloc
Invocation form: (type specifier *) malloc (size) function: Allocates a contiguous region of length "size" bytes in the dynamic storage of memory.
The return value of the function is the first address of the range. The type specifier indicates what data type to use for the region. The (type specifier *) indicates that the return value is cast to the type pointer. ' Size ' is an unsigned number. For example: pc= (char *) malloc (100); Represents a 100-byte allocation of memory space and is cast to a character array type. The return value of the function is a pointer to the array of characters. Assign the pointer to the pointer variable PC.
2. Allocating memory space functions Calloc
Calloc is also used to allocate memory space. Invocation form: (type specifier *) calloc (n,size) function: Allocates n contiguous regions of length "size" bytes in the memory dynamic store.
The return value of the function is the first address of the range. (The type descriptor *) is used to force type conversions.
The Calloc function differs from the malloc function only in the ability to allocate n blocks at a time. For example: ps= (Struet stu*) calloc (2,sizeof (struct stu)); The sizeof (struct Stu) is the structural length of the Stu. So the statement means: Allocate 2 contiguous regions by the length of the Stu, cast to the Stu type. and assigns its first address to the pointer variable PS.
To put it simply:
malloc it agrees to allocate memory from the spatial memory pool, and the parameter of malloc () is an integer that specifies the number of bytes required.
For example: p= (int*) malloc (n*sizeof (int));
Colloc is similar to malloc, but the basic difference is that the value stored in the allocated memory space defaults to 0, and when malloc is used, the allocated memory can be arbitrary.
Colloc requires two parameters, the first is the number of variables that need to be allocated memory, and the second is the size of each variable.
For example: p= (int*) colloc (n,colloc (int));
There is also a version number:
Function prototypes are different:
void *malloc (unsigned size)//Dynamic request size bytes of memory space; function : Allocates a contiguous region of length "size" bytes in the dynamic storage of memory.
The return value of the function is the first address of the range.
。 The (type specifier *) indicates that the return value is cast to the type pointer.
(void *) calloc (unsigned n,unsigned size)//used to dynamically apply to the system n, each of the memory space accounted for a size byte; and initialize the allocated memory to a value of 0. The return value of the function is the first address of the range
(void *) realloc (void *p,unsigned size)//change the size of the allocated memory area pointed to by the pointer p to size
difference: Both are dynamically allocated memory.
The basic difference is that malloc does not initialize the allocated memory, and the allocated memory can be random values. Calloc initialized the allocated memory to 0. The minor difference is that calloc returns an array, and malloc returns an object.
malloc it agrees to allocate memory from the spatial memory pool, and the parameter of malloc () is an integer that specifies the number of bytes required.
For example: p= (int*) malloc (n*sizeof (int));
Colloc is similar to malloc, Colloc requires two parameters, the first is the number of variables that need to be allocated memory, and the second is the size of each variable.
For example: p= (int*) colloc (n,sizeof (int));
example, request a pointer of a character size
Char *p= (char *) malloc (sizeof (char)); Of course the individual is meaningless to apply a dynamic array or a structure, such as
Char *str= (char *) malloc (sizeof (char) *100); Equivalent to the static character array str[100], but the size can change the
typedef struct POINTER
{int data;
struct pointer *p;
} pp;
PP *p= (pp *) malloc (sizeof (struct pointer)); Dynamic application of structural body space
Several other functions are the basis for the team to apply for changes to the space to define how you can try
Another version number:
Http://nuomi1988.blog.hexun.com/35121805_d.html
One: They are all dynamically allocated memory. Let's look at their prototypes first:
void *malloc (size_t size); Size of allocation
void *calloc (size_t numelements, size_t sizeofelement); The number of allocated elements and the size of each element
The common denominator is that they return a void * type, which means that we must explicitly cast the space for an int or other type of data allocation;
The difference is that when allocating storage space with malloc, we must calculate the required number of bytes. Suppose you want to allocate a space of 5 int, that is, the memory space that requires 5*sizeof (int):
int * IP_A;
Ip_a = (int*) malloc (sizeof (int) * 5);
And the use of calloc does not need to be so calculated, directly:
Ip_a = (int*) calloc (5, sizeof (int)), so that the corresponding space is allocated, and the biggest difference between them is that the only space allocated with malloc is not initialized. That is, the data in this memory is still preserved, and calloc is initialized. Calloc allocated space all initialized to 0. This avoids some of the possible data errors.
Write a piece of code experience experience ....
#include <iostream>
using namespace Std;
void Main ()
{
int * IP_A;
int * IP_B;
Ip_a = (int*) malloc (sizeof (int) * 5);
for (int i = 0; i < 5; i++)
{
cin>>ip_a[i];
}
for (int j = 0; J < 5; J + +)
{
cout<<ip_a[j]<< "";
}
Ip_b = (int*) calloc (5, sizeof (int));
for (int j = 0; J < 5; J + +)
{
cout<<ip_b[j]<< "";
}
}
This output is a clear look at their differences ....
+ + version:
the declarations of the three functions are each:
void* realloc (void* ptr, unsigned newsize);
void* malloc (unsigned size);
void* calloc (size_t numelements, size_t sizeofelement);
are in the Stdlib.h function library .
Their return values are the addresses that are requested by the system and return NULL if the request fails
malloc is used to apply for a new address, where size is the length of the memory space required, such as:
Char* p;
p= (char*) malloc (20);
Calloc is similar to malloc, where sizeofelement is the unit element length of the application address, numelements is the number of elements, such as:
Char* p;
p= (char*) calloc (20,sizeof (char));
This example is the same as the previous effect
ReAlloc is to assign space to a pointer that has already been assigned an address, PTR is the original space address, and newsize is the address length of another application.
Such as:
Char* p;
p= (char*) malloc (sizeof (char) *20);
p= (char*) realloc (p,sizeof (char) *40);
Note that the space length here is in bytes.
Standard memory allocation function for the C language: malloc. Calloc,realloc,free and so on.
The difference between malloc and calloc is 1 blocks vs. N blocks:
The malloc invocation form is (type *) malloc (size): Allocates a contiguous region of length "size" in the dynamic store of memory, returning the first address of the zone.
The Calloc invocation form is (type *) Calloc (n. Size): Allocates an n block of contiguous bytes of length "size" in the dynamic store of memory, returning to the first address.
ReAlloc Call form (type *) realloc (*ptr,size): Increases PTR memory size to size.
Free Call form free (VOID*PTR): Publishes the memory space that PTR points to.
C + + is a new/delete feature.
malloc () and calloc differences