D
D
Demigodd2018-04-12 08:45:35
C++ / C#
Demigodd, 2018-04-12 08:45:35

What is the difference between malloc() and calloc() and free() and dellete()?

Let's allocate memory

int *x = (int*) malloc(10);
int *y = (int*) calloc(10, sizeof(int));

They have 2 differences
calloc allocates memory for the data array, pre-initializing it with zeros. takes 2 arguments.
malloc allocates memory without initialization. takes one argument.
But in principle, both of these functions do the same thing, what is their difference?
I read somewhere that calloc creates cells of a certain type and combines them, for example, int can be 4 bytes, and if we write Then it will create 12 cells, 4 4 4 and each of the 4 bytes will store the number 0. It will also create 12 cells, but if, for example, we add the first 4 byte to it, the number 9, to another byte "string", and to the other generally bool, how will it determine what is in it? If in the case of calloc he knows that every 4 bytes he has a digit, then as in
int *y = (int*) calloc(3, sizeof(int));
int *x = (int*) malloc(12);
malloc will determine that after every different number of bytes it has a different number of elements, Or does it define its elements in a completely different way, at compile time, etc.?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
res2001, 2018-04-12
@Demigodd

The only difference is that calloc zeros out the allocated memory before returning the pointer, while malloc does not. Inside calloc, it probably calls malloc to allocate memory, and then memset to zero. So calloc is just an add-on to malloc for convenience. Here is a schematic implementation of calloc:

void * calloc (size_t num, size_t size)
{
   void * mem = malloc(num * size);
  memset(mem, 0, num * size);
  return mem;
}

These functions themselves operate exclusively with the size of the allocated area in bytes, they do not care what you will do with the allocated memory in the future - write ints or "strings" there.
Well, no matter how no one bothers you to use the same memory area first as an array of ints, and then as an array of bytes. Or like this for example:
int a = 0x33323130;
char * c = (char*)&a;
printf("%c %c %c %c\n", c[0], c[1], c[2], c[3]);

The example does not use malloc/calloc to allocate memory, memory is allocated on the stack simply by declaring int a. Here I tried to show that the contents of the memory can be interpreted in any way, the main thing is within the boundaries of the allocated range.
Moreover, C / C ++ / asm languages ​​​​allow this, while others do not.
The example assumes that an int has a size of 32 bits, which is not the case for all platforms, but in general it is.
By the way, this example can be used to determine the byte order of the platform: if "1 2 3 4" is displayed, then you have LITTLE ENDIAN, and if "2 1 4 3" - BIG ENDIAN.
PS: free() is C and delete is C++

D
Denis Zagaevsky, 2018-04-12
@zagayevskiy

The difference is in filling with zeros (initialization). About the definition of what is where, I did not understand. C does not track this at all. Both functions give you absolutely identical pointers, what to do with them is up to you.
About different signatures - they say that calloc, when multiplying arguments, monitors integer overflow and returns NULL in the case when it happened. With malloc, you have to do it yourself.

A
Antonio Solo, 2018-04-12
@solotony

if you are programming in C++, then it is better not to allocate memory using malloc/calloc, but to create arrays of objects or base types using new . and for direct work with memory it is necessary to have very weighty substantiation.
there is no such dellete() there is a delete
operator that releases what is allocated new

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question