-1

*Note: This is not a duplicate question, since the answer you refer to does not answer my question. I know what malloc() and calloc() should do, but wonder why there doesn't seem to be a difference when using it with Virtual Machine.

I know what the difference should be - malloc() just allocate you the memory, while calloc() initialize it with 0's.

The thing is, in my code it doesn't show up, and malloc() doesn't seem to be giving any difference while running from my Virtual Machine Ubuntu. I ran it a few times, and malloc acts exactly like calloc.

Note - I just checked it with my actual hard drive, and it seems to work ok, there I do get different results.

The code:

#include <stdio.h>
#include <stdlib.h>

int main(){
int i,n;
float *ptr1, *ptr2;
printf("enter a total number of float items: ");
scanf("%d", &n);

ptr1 = malloc(n*sizeof(float));
ptr2 = calloc(n, sizeof(float));

printf("malloc     |    calloc\n");
printf("----------------------\n");
for(i=0;i<n;i++)
printf("%-10f  %10f\n", *(ptr1+i), *(ptr2+i));
printf("\n");
free(ptr1);
free(ptr2);
return 0;
}
Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66
  • 2
    What do you mean acts exactly like calloc? You're getting zeroes? It's allowed, nothing says you *wouldn't* get. But nothing says you *will* get zeroes either. – Sami Kuhmonen Jul 22 '16 at 08:24
  • 1
    OT: Referring the code's call to `printf()`: Memory returned by `malloc()` had not been initialised. Printing the content of uninitialised memory invokes undefined behaviour. – alk Jul 22 '16 at 08:38
  • Simple reason: The pages are cleared for security reasons by the OS. You shall never rely on it, though. – too honest for this site Jul 22 '16 at 08:39
  • [I got the dupe by a google search, the very first hit.](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=difference+between+malloc+and+calloc) – Sourav Ghosh Jul 22 '16 at 09:10
  • 1
    Try allocating some large amounts of memory with `malloc`, setting it to some non-zero value, then freeing it. Then call `malloc` again and check to see if the returned memory overlaps with the earlier areas. If so, then you should most likely see non-zero values. – Tom Karzes Jul 22 '16 at 09:11
  • Souarv, read the question thoroughly and see if you can understand why it's not a duplicate. – Maverick Meerkat Jul 22 '16 at 10:17

2 Answers2

6

calloc guarantees you to have a zeroed memory chunk, while malloc not. Then it may happen that malloc does it, but never rely on it.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

The memory obtained from malloc can contain anything. It might contain zeroes or it might contain something else.

The following example shows (on most platforms) that memory returned from malloc has not been set to 0:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  char *p;
  int i;

  for (i = 0; i < 1024; i++) {
    p = malloc(1024);
    strcpy(p, "something else");
    free(p);
  }
  p = malloc(100);  // Get a fresh memory block
  printf("%s\n", p);  // You should not access the memory returned from malloc without assigning it first, because it might contain "something else".
  return 0;
}

The program could do anything, we don't even know if the string is NUL-terminated, but on most platforms the output will be:

something else

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82