lmiguelvargasf
lmiguelvargasf

Reputation: 69873

What does it mean to initialize memory to zero in C?

I am reading about pointers and dynamic memory allocation in C. I have found that the function calloc() is similar to malloc() but the former does initialize memory to 0.

I do not understand why does it mean to initialize memory to zero?

NOTE: I am not asking the difference between malloc and calloc, but the meaning of initializing memory to zero.

Thanks in advance.

Upvotes: 2

Views: 16744

Answers (7)

user6238400
user6238400

Reputation:

As @Blorgbeard says in one comment. I just means that every byte in the block you are allocating is going to be set to 0x00.

Upvotes: 1

The Quantum Physicist
The Quantum Physicist

Reputation: 26326

It just means that all bits will be set to zero after reserving that memory block. What the consequence is depends on your interpretation of the data. For example, If you cast these to memory addresses/pointers, they'll be equivalent to NULL. If you cast them to some integer type, they'll be equivalent to zero valued integers.

Whether you want to use malloc() and keep garbage values (so just reserve memory without modifying its values from previous time) or use calloc() to get a clean memory block depends on what you're planning to do with this memory block. Normally the cost to set all memory values to zero is negligible when talking about normal software, but if you're developing an extremely high performance software, then every bit matters, and you have the freedom to use these this memory with or without setting it to zero.

Upvotes: 2

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29223

Memory that's initialized to zero is memory in which every addressable object (every byte), will always have a value equal to zero, when you read it as an non-padded type (e.g. unsigned char),

It's useful in the sense that you always get the same memory contents (instead of "random" garbage). This eliminates the possibility that this memory chunk could have acted as a potential source of non-deterministic behavior of your program. Deterministic behavior helps you catch errors earlier.

Upvotes: 2

Scott Morken
Scott Morken

Reputation: 1651

When you allocate memory, you are issuing a request to your operating system to reserve a block of memory for your program's use. Simply reserving the memory doesn't necessarily mean that whatever data was in the memory already will be initialized.

Upvotes: 2

axiac
axiac

Reputation: 72256

The memory block returned by malloc() contains whatever data happened to be in that memory; it contains leftovers of the previous usage of that memory.

Before returning, calloc() sets to 0 all the bytes of the memory block it returns.

Upvotes: 5

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137467

p = calloc(12, 1024);

Is roughly equivalent to:

p = malloc(12 * 1024);
if (p != NULL)
    memset(p, 0, 12 * 1024);

So calloc does two things that malloc does not:

  • It multiplies the nmemb and size variables to calculate the total size of the allocation
  • It sets every byte of the allocation to zero

For this purpose, calloc is good for allocating arrays, particular arrays of scalar values.

There are cases where this initialization is undesirable:

  • Initialization to all-zero bits is insufficient for pointers, where NULL may not be all zeros.
  • If you're going to use the allocation as a buffer and will be reading data into it, then it's a waste of time to initialize it to all-zeros first.

Upvotes: 13

Kerrek SB
Kerrek SB

Reputation: 477338

Every object has an underlying representation as a sequence of bytes in memory. It is those bytes that have value zero when they are obtained via calloc. What that means for particular objects depends on how they are represented in memory. For example, uint16_t is guaranteed to occupy 16 bits without padding, and the representation bits make up its value, so *(uint16_t*) calloc(sizeof(uint16_t), 1) is the object that holds value zero. By contrast, if you obtain the memory via malloc, you must not attempt to read from it, but rather you must assign a value to it first.

Upvotes: 3

Related Questions