Questions tagged [calloc]

The calloc function performs dynamic memory allocation in C, and is part of the standard library.

The standard C library declares the function calloc() as follows:

void *calloc(size_t elements, size_t sz);

calloc() allocates space for an array of elements, each of which occupies sz bytes of storage. The space of each element is initialized to binary zeros. In other words, calloc() is similar to malloc(), except that it handles arrays of objects rather than a single chunk of storage and that it initializes the storage allocated. The following example allocates an array of 100 int's using calloc():

int * p = (int*) calloc (100, sizeof(int));

NAME

calloc - a memory allocator

SYNOPSIS

#include <stdlib.h>

void *calloc(size_t nelem, size_t elsize);

DESCRIPTION

The calloc() function allocates memory for an array of nelem elements of elsize bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nelem or elsize is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

RETURN VALUE

The calloc() function returns a pointer to the allocated memory that is suitably aligned for any kind of variable. On error, it returns NULL. NULL may also be returned by a successful call with nelem or elsize equal to zero.


Wikipedia

References

Related Tags

557 questions
13
votes
3 answers

preferring malloc over calloc

Possible Duplicate: c difference between malloc and calloc Is there any situation where you would prefer malloc over calloc. i know both malloc and calloc allocate memory dynamically and that calloc also initializes all bits in alloted memory to…
user514946
  • 1,165
  • 3
  • 12
  • 12
12
votes
7 answers

calloc -- Usefulness of zeroing out memory

What is the advantage of zeroing out memory (i.e. calloc() over malloc())? Won't you change the value to something else anyways?
chrisgoyal
  • 4,297
  • 4
  • 22
  • 27
9
votes
2 answers

Why calloc takes two arguments while malloc only one?

IMO one is enough, why does calloc require to split it into two arguments?
x86
  • 113
  • 1
  • 3
9
votes
1 answer

Are there any advantages to using calloc() instead of a malloc() and memset()?

I was wondering whether calloc() is preferable to a malloc followed by a memset. The latter appears to be the most common way of allocating and initializing memory. A github code search turns up many calloc test and implementations but in the first…
Kevin Cox
  • 3,080
  • 1
  • 32
  • 32
9
votes
4 answers

is it necessary to type-cast malloc and calloc

Possible Duplicate: Do I cast the result of malloc? I was googling to find out the reason for type-casting of malloc and calloc. But, i only found type-casting of malloc is not necessary since it return void pointer but, what about calloc. This…
Ravi
  • 30,829
  • 42
  • 119
  • 173
8
votes
5 answers

Does any operating system implement buffering for malloc()?

A lot of c/malloc()'s in a for/while/do can consume a lot of time so I am curious if any operating system buffers memory for fast mallocs. I have been pondering if I could speed up malloc's by writing a "greedy" wrapper for malloc. E.g. when I ask…
Waxhead
  • 500
  • 3
  • 16
7
votes
2 answers

Does the Meltdown mitigation, in combination with `calloc()`s CoW "lazy allocation", imply a performance hit for calloc()-allocated memory?

So calloc() works by asking the OS for some virtual memory. The OS is working in cahoots with the MMU, and cleverly responds with a virtual memory address which actually maps to a copy-on-write, read-only page full of zeroes. When a program tries to…
7
votes
3 answers

Valgrind says "stack allocation," I say "heap allocation"

I am trying to trace a segfault with valgrind. I get the following message from valgrind: ==3683== Conditional jump or move depends on uninitialised value(s) ==3683== at 0x4C277C5: sparse_mat_mat_kron (sparse.c:165) ==3683== by 0x4C2706E:…
Joel J. Adamson
  • 713
  • 4
  • 10
7
votes
2 answers

"Heap corruption detected" when using free()

I'm pretty new to C (it's actually my first assignment with pointers), and I cant figure out this bug... here is my code: void str_rv(char c[]) { int i, len = str_ln(c); char *rev = (char*)calloc(len, sizeof(char)); check_mem(rev); …
Tomer Amir
  • 1,515
  • 4
  • 27
  • 54
7
votes
1 answer

calloc() slower than malloc() & memset()

I would like to ask you a question. I have the following code: #include #include #include #define XXX 1024*1024 int main() { int *p; unsigned long x=0; while (1) { //p…
Alex
  • 635
  • 6
  • 15
6
votes
4 answers

Understanding the purpose of malloc and calloc

I'm trying to get my head around C. Reading through K&R, I am flicking back and forth trying to find where it states the situations I should obtain blocks of memory dynamically. For example, I want to have an int pointer. int *pointer; But then K&R…
user485498
6
votes
4 answers

Does calloc() of a double field always evaluate to 0.0?

Does calloc() of a double field always evaluate to 0.0? Furthermore: Does calloc() of a float field always evaluate to 0.0f? Does calloc() of an int or unsigned int field always evaluate to 0? That is, will the assert() below always succeed on…
u17
  • 2,776
  • 4
  • 31
  • 43
6
votes
4 answers

Does the pointer passed to free() have to point to beginning of the memory block, or can it point to the interior?

The question is in the title... I searched but couldn't find anything. Edit: I don't really see any need to explain this, but because people think that what I'm saying makes no sense (and that I'm asking the wrong questions), here's the…
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
4 answers

Does CUDA really not have a calloc()-like API call?

From looking at the CUDA 5.5 API Reference and the CUDA C Programming Guide it seems that there is no cudaCalloc(), an on-GPU equivalent of the standard C library's calloc(). Is there really no API functionality for allocating a buffer initialized…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
1 answer

calloc fails and returns NULL

in one of our application's module, calloc() is failing and returning NULL. The amount of memory that it is trying to allocate is of structure which is of 9292 bytes. The operating system is AIX 7.1 and running VIOS 2.2.1.3. The machine has 2+GB ram…
kuldeep
  • 817
  • 10
  • 27
1
2
3
37 38