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

489 questions
827
votes
14 answers

Difference between malloc and calloc?

What is the difference between doing: ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or: ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc over malloc or vice versa?
user105033
  • 16,546
  • 15
  • 55
  • 66
86
votes
6 answers

Proper way to initialize C++ structs

Our code involves a POD (Plain Old Datastructure) struct (it is a basic c++ struct that has other structs and POD variables in it that needs to get initialized in the beginning.) Based one what I've read, it seems that: myStruct =…
Shadow503
  • 1,014
  • 1
  • 10
  • 14
49
votes
5 answers

What does the first "c" stand for in "calloc"?

A student asked the question and I didn't know for sure. Guesses include: "counted", "clearing", "chunked", "complete", ... The standard library documentation doesn't say what it stands for and there aren't similarly named functions that would…
Raymond Hettinger
  • 182,864
  • 54
  • 321
  • 419
42
votes
5 answers

Invalid application of sizeof to incomplete type with a struct

I have a struct where I put all the information about the players. That's my struct: struct player{ int startingCapital; int currentCapital; int startingPosition; int currentPosition; int activePlayer; int canPlay; }; And…
captain
  • 1,597
  • 4
  • 17
  • 29
38
votes
11 answers

C++: new call that behaves like calloc?

Is there a call I can make to new to have it zero out memory like calloc?
nivnad
  • 666
  • 1
  • 5
  • 10
33
votes
4 answers

C - calloc() v. malloc()

Possible Duplicate: c difference between malloc and calloc Please explain the significance of this statement, Another difference between the malloc() and calloc() functions is that the memory allocated by malloc( ) function contains…
Kevin Meredith
  • 38,251
  • 58
  • 190
  • 340
32
votes
5 answers

Two arguments to calloc

Why does calloc take two arguments instead of one like malloc? Specifically, since there is no difference between (or is there?) between the following expressions: calloc (a, b); calloc (b, a); calloc (a * b, 1); calloc (1, a * b); why not just…
sanjoyd
  • 3,012
  • 2
  • 13
  • 22
29
votes
5 answers

I'm very confused about malloc() and calloc() on C

I've always programmed in Java, which is probably why I'm so confused about this: In Java I declare a pointer: int[] array and initialize it or assign it some memory: int[] array = {0,1,0} int[] array = new int[3] Now, in C, it's all so confusing.…
bluehallu
  • 9,845
  • 8
  • 38
  • 59
22
votes
6 answers

Proper usage of realloc()

From man realloc:The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. So in this code snippet: ptr = (int *)…
user3163420
  • 235
  • 1
  • 2
  • 6
18
votes
1 answer

why is there no aligned calloc in C11

The C11 standard added the aligned_alloc function to allocate uninitialized aligned memory. The standard also includes the calloc function to allocate memory which is initialized to zero but only aligns it to the size of the largest type. Why does…
jtaylor
  • 2,079
  • 13
  • 18
16
votes
7 answers

calloc v/s malloc and time efficiency

I've read with interest the post C difference between malloc and calloc. I'm using malloc in my code and would like to know what difference I'll have using calloc instead. My present (pseudo)code with malloc: Scenario 1 int main() { allocate…
yCalleecharan
  • 4,468
  • 9
  • 52
  • 82
15
votes
5 answers

Is calloc better than malloc?

I have just learned about the C calloc() function the other day. Having read its description and how it differs from malloc (1, 2), I get the idea that, as a non-embedded programmer, I should always use calloc(). But is that really the case? One…
Violet Giraffe
  • 29,070
  • 38
  • 172
  • 299
14
votes
2 answers

Why is there no "recalloc" in the C standard?

Everyone knows that: realloc resizes an existing block of memory or copies it to a larger block. calloc ensures the memory is zeroed out and guards against arithmetic overflows and is generally geared toward large arrays. Why doesn't the C…
Matt
  • 17,895
  • 16
  • 55
  • 102
13
votes
3 answers

Calloc with structure with pointers in C

I know that calloc request memory to be used, writes 0 on all the bits and then returns a pointer to it. My question is: if I use calloc with a structure that contains pointers, will those pointers have the NULL value or do I have to set them to…
Mihai Neacsu
  • 1,955
  • 5
  • 20
  • 36
12
votes
4 answers

Multidimensional arrays allocated through calloc

I have a question about how memory is allocated when I calloc. I had a look at this question, but it doesn't address how memory is allocated in the case of a dynamically allocated two dimensional array. I was wondering if there was a difference in…
Kitchi
  • 1,724
  • 4
  • 25
  • 46
1
2 3
32 33