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
5
votes
1 answer

Why is np.zeros() faster than re-initializing an existing array in Numba with Python?

Why isnumpy.zeros() faster than re-initializing an existing array? I work with computer modeling and use numba for my work. Sometimes it is necessary to have a zeroed array to accumulate the results of some operation. In general, I suppose that…
5
votes
3 answers

Why memset called after calloc?

I've researched code of some library and noticed that calls to calloc there are followed by memset for block allocated by calloc. I've found this question with quite comprehensive answer on differences between calloc and malloc + memset and calling…
toozyfuzzy
  • 1,080
  • 1
  • 9
  • 20
5
votes
3 answers

gcc7.2: argument range exceeds maximum object size 9..7 [-Werror=alloc-size-larger-than=]

The program contains code like follows: int size; ... int *pi = (int*)calloc(size, sizeof(int)); ... Here is the error message when compiled with gcc7.2: error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object…
Yuan Wen
  • 1,583
  • 3
  • 20
  • 38
5
votes
6 answers

free() function without malloc or calloc

quick question Can you use the free() function without having to prior call a malloc ?? ei. void someFunc( void ) { char str[6] = {"Hello"}; //some processing here .... free(str); } I get no compiling errors but Does this work or is it…
jramirez
  • 8,537
  • 7
  • 33
  • 46
5
votes
3 answers

C - How to use own value (instead of 0) with calloc

In C when we use the calloc method; all the reserved memory will be initialized to 0. Is there any way to initialize it with another value without iterating over all the values? For example: int* example = calloc(100,sizeof(int)); This will create…
Domien
  • 395
  • 5
  • 18
5
votes
2 answers

calloc() and NULL

I know that calloc allocates memory and writes zeroes to each cell, so my question is: is there a difference between using calloc or using malloc and running over the cells writing NULL to them? Are the zeroes of calloc equivalent to NULL?
yoniyes
  • 1,000
  • 11
  • 23
5
votes
4 answers

calling calloc - memory leak valgrind

The following code is an example from the NCURSES menu library. I'm not sure what could be wrong with the code, but valgrind reports some problems. Any ideas... ==4803== 1,049 (72 direct, 977 indirect) bytes in 1 blocks are definitely lost in loss…
Mike
  • 53
  • 1
  • 4
5
votes
4 answers

What is the difference between calloc(10,4) and calloc(1,40)?

What is the difference between calloc(10,4) and calloc(1,40)? I see this behavior: Thing** things = (Thing**)calloc(1, 10 * sizeof(Thing*)); // things[0] != 0 Thing** things = (Thing**)calloc(10, sizeof(Thing*)); // things[0] == 0 I would like to…
NateS
  • 5,751
  • 4
  • 49
  • 59
5
votes
2 answers

flexible mpz_t array in struct

I have a struct like this: typedef struct{ size_t length; // length of the array size_t numbits; // number of bits allocated per val in vals mpz_t vals[]; // flexible array to hold some number of mpz_t array } CoolArray; Ok, so it's a…
Broseph
  • 1,655
  • 1
  • 18
  • 38
5
votes
3 answers

calloc(): Do the individual values matter for performance?

I'm currently writing an embedded application in C where performance is critical. Currently, I'm allocating lots of empty memory like this: calloc(1, num_bytes) - however, I simply calculate num_bytes as the product of a number of items and the size…
Thomas O
  • 6,026
  • 12
  • 42
  • 60
4
votes
3 answers

Initialising an array of structs in Objective-C

I've been reading about this for a while and I'm not sure I have found a good answer. I'm trying to setup an array of 92 structs. It's a fixed length and will not change as it's effectively a lookup table. I thought that the best way to do this was…
drekka
  • 20,957
  • 14
  • 79
  • 135
4
votes
0 answers

Hint compiler that void * points to zeroed memory

Many modern architectures use type with all bits set to zero, in which case: void *p; memset(&p, 0, sizeof(p)); assert(p == NULL); /* assertion holds on most architectures */ Just to be fully compliant I also explicitly set all pointers inside…
Wirtos_new
  • 41
  • 1
  • 2
4
votes
3 answers

Does calloc zero out the entire allocation?

The calloc function in C is used to allocate zeroed memory capable of holding at least the requested amount of elements of a specified size. In practice, most memory allocators may allocate a bigger block in order to increase efficiency and minimize…
Aiueiia
  • 162
  • 9
4
votes
1 answer

What are the advantages and disadvantage of using jemalloc vs malloc vs calloc and other common alternatives?

Reading the Rust subreddit today I came across comments that: jemalloc is optimized for (multithreaded) speed, not memory usage After doing more research I found that there are even more alternatives (such as calloc). I would like to understand…
Greg
  • 8,175
  • 16
  • 72
  • 125
4
votes
2 answers

File to dynamic array in c

DISCLAIMER: I'm new to C. What is the best way to convert every line in a .txt file (can be other file types too) to a dinamic calloc() array, and the other way around? In my file I have to following: 1 Hello 2 18 3 World 4 15 etc... I want…
user5805283
1 2
3
37 38