what is the difference between malloc , calloc and realloc?
what r their uses?

Recommended Answers

All 11 Replies

malloc returns a block of memory that is allocated for the programmer to use, but is uninitialized. calloc allocates memory and then initializes it. realloc is used to grow or shrink a block of memory.

malloc returns the blocks in random way(dynamic)
calloc returns the blocks in sequencial way means one after another
realloc is used to minimise already allocated memory blocks or maximize the same

commented: You resurrected a 2 year old thread for this incorrect information? Great! -4

In C malloc() returns bytes of memory which is need not to be contiguous.
like

int *a;
a=(int*) malloc(sizeof(int));

It returns some bytes (4 byte in some compiler) which is uninitialized.
Let take the address of a is 1002. If we try to assign memory to some other int pointer it may not be 1006.
on the other hand calloc() returns a block of memory which is contiguous and initialized to zero.


realloc() use to reallocate the memory which is already allocated in a dynamic way.

malloc returns the blocks in random way(dynamic)
calloc returns the blocks in sequencial way means one after another
realloc is used to minimise already allocated memory blocks or maximize the same

That's the dumbest thing I've read all morning. But it's early, so I'm sure someone will come up with something better. Now to correct your misconceptions.

malloc returns the blocks in random way(dynamic)

WTF is that supposed to mean? Sure, it might seem random due to the strategies memory managers will use, but I fail to see how that's even moderately useful to you as the end programmer. Each pointer returned by malloc() is unique on success or NULL on failure, and that's all you can assume.

calloc returns the blocks in sequencial way means one after another

malloc() and calloc() are identical with the exception that calloc() initializes the bytes to zero. A logical implementation is thus:

void *calloc(size_t count, size_t size)
{
    size_t total = count * size;
    void *mem = malloc(total);

    if (mem != NULL)
        memset(mem, 0, total);

    return mem;
}

I've seen your mistake before, and it typically stems from the way calloc() separates item count and item byte size while malloc() only takes a total byte count. However, that's only a superficial difference; semantically the effect is the same.

realloc is used to minimise already allocated memory blocks or maximize the same

realloc() does one of three things:

  • Simulate malloc() if the first argument is NULL.
  • Simulate free() if the second argument is 0.
  • "Resize" memory pointed to by the first argument to the size given in the second argument and return the result.

I say "resize" because realloc() is allowed to move and copy in all cases:

void *realloc(void *p, size_t size)
{
    if (size == 0) {
        free(p);
        return NULL;
    }
    else if (p == NULL) {
        return malloc(size);
    }
    else {
        void *new = malloc(size);

        if (new != NULL) {
            memcpy(new, p, __block_size(p));
            free(p);
        }

        return new;
    }
}

A smart implementation may try to resize in-place and return the same address, but you can't make that assumption. This is why it's unsafe to rely on pointers into the block; they may be invalidated if the block is moved.

In C malloc() returns bytes of memory which is need not to be contiguous.

The bytes allocated by malloc() (and calloc()) are required to be contiguous.

commented: yeah, and it's 3.5 years after the fact, too. +17

malloc() , calloc() and realloc() all these functions are for dynaminc allocation in memory.
basic differences are in malloc() single parameter is passed which is only the size of allocated memory but in calloc() two parameters are passed which is the number of blocks to be taken and another one is size.
malloc() initializes garbage value as a default while calloc() has zero as its default value.

and realloc() is used for reallocating the used memory either to increase or decrease the memory.

commented: How many times do we have to restate the same information answering a 3.5 year old thread? -4

The bytes allocated by malloc() (and calloc()) are required to be contiguous.

int *p,*a;
p=(int*)malloc(sizeof(int));
a=(int*)malloc(sizeof(int));

here memory need not to be contiguous.

int *p;
p=(int*)malloc(5*sizeof(int));

here memory allocation must be contiguous.

commented: How many times do we have to restate the same information answering a 3.5 year old thread? -4
int *p,*a;
p=(int*)malloc(sizeof(int));
a=(int*)malloc(sizeof(int));

here memory need not to be contiguous.

int *p;
p=(int*)malloc(5*sizeof(int));

here memory allocation must be contiguous.

Are you an idiot? How does calling malloc() twice somehow dispute my statement that the bytes allocated by malloc() are required to be contiguous? Further, how does it prove your incorrect statement that malloc() returns memory which isn't contiguous while calloc() does? Are you aware that the exact same code using calloc() has precisely the same result?

/* Not contiguous */
int *p,*a;
p=(int*)calloc(1, sizeof(int));
a=(int*)calloc(1, sizeof(int));
/* Contiguous */
int *p;
p=(int*)calloc(5, sizeof(int));

If anything this proves my point that malloc() and calloc() differ only in number of arguments and a subsequent memset()-like effect inside calloc(). Though you're really not comparing the same thing with those examples, the difference is between the dynamic memory (heap) manager and the compiler's stack frame layout. Nobody with any knowledge of how memory works in C would expect them to be related.

Are you aware that the exact same code using calloc() has precisely the same result?

Yes i am aware about the fact.

by me:-calloc() returns a block of memory which is contiguous

I was talking about:-

/*contiguous*/
int *p;
p=(int*)calloc(5, sizeof(int));

I also want to mean that malloc() is not contiguous for different allocation.

/*not contiguous*/
int *p,*a;
p=(int*)malloc(sizeof(int));
a=(int*)malloc(sizeof(int));

Any way i am a newbie and just a learner while you are an administrator. I may have represented the thing which can may create confusions as I should write calloc() returns a block of memory which may or mayn't contiguous instead, but calling any one idiot in a forum like this may hurt any one's prestige and feeling.
I request you to kindly delete my account from this forum.:icon_sad:

I also want to mean that malloc() is not contiguous for different allocation.

That's no different from saying this:

int a, b; /* Not necessarily contiguous */
int c[5] /* Contiguous */

It has nothing at all to do with malloc().

I request you to kindly delete my account from this forum.

We don't delete accounts as a way to conform with anti-spam regulations. If you're really that sensitive, you should probably refrain from surfing the web entirely. Or you could just suck it up, learn what I'm teaching, and actually benefit rather than run off crying like a child who can't get his way.

That's also not different from saying this:

char a, b; /* Not necessarily contiguous */
char c[5] /* Contiguous */

Its also has nothing to do with int , calloc or malloc.

I am not being childish, I am not understanding for what misunderstanding we are arguing on the same fact which we are talking about.

I am not understanding for what misunderstanding we are arguing on the same fact which we are talking about.

That's obvious. Let me start again as a final attempt. You said this:

In C malloc() returns bytes of memory which is need not to be contiguous.
[...]
on the other hand calloc() returns a block of memory which is contiguous and initialized to zero.

These statements are incorrect, and your justification for them is nonsensical. You obviously understand that the block returned by both malloc() and calloc() consists of contiguous bytes, which is correct, so leave it at that.

If you want to say that the blocks returned by multiple calls are not required to be contiguous, that's correct as well, but doesn't in any way make the quoted statements correct as you seem to have intended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.