-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; } 
6
  • 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. Commented Jul 22, 2016 at 8: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. Commented Jul 22, 2016 at 8:38
  • Simple reason: The pages are cleared for security reasons by the OS. You shall never rely on it, though. Commented Jul 22, 2016 at 8:39
  • I got the dupe by a google search, the very first hit. Commented Jul 22, 2016 at 9: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. Commented Jul 22, 2016 at 9:11

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.