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
4
votes
2 answers

Between malloc and calloc which allocates contiguous memory

I read so many links regarding malloc and calloc but still Iam bit confused about "Between malloc and calloc which allocates contiguous memory". In some links they have given malloc allocates contiguous memory as a block of bytes But in some links…
bvb
  • 633
  • 2
  • 7
  • 15
4
votes
6 answers

When to free memory inside a C code?

When I alloc memory outside a while loop for example, is it okay to free it inside it ? Are these two codes equivalent ? int* memory = NULL; memory = malloc(sizeof(int)); if (memory != NULL) { memory=10; free(memory); } int* memory =…
nour02
  • 43
  • 1
  • 3
4
votes
5 answers

String parsing in C using strtok

I've got this little source code, made for testing the parsing of a string similar to variable string I need to use in other project #include #include #include int main (void) { char string[] =…
Arrigo Pierotti
  • 203
  • 4
  • 10
  • 18
4
votes
3 answers

C struct padding on initialization

I have a struct such as typedef struct { int a; // Let's say this ends up being 4 bytes int b; // 4 bytes char text[10]; // 10 bytes } blah_t; static blah_t myvar; Suppose the sum of fields’ sizes is 18 bytes in blah_t, but…
B. Nadolson
  • 2,988
  • 2
  • 20
  • 27
4
votes
1 answer

Qt Creator - calloc fails with large memory

I have a problem with Qt Creator, or one of its components. I have a program which needs lots of memory (about 4 GBytes) and I use calloc to allocate it. If I compile the C code with mingw/gcc (without using the Qt-framework) it works, but if I…
user1703919
  • 43
  • 1
  • 4
3
votes
4 answers

Segmentation fault by writing in a 2D Array

I have a small memory access problem in my program and I do not find the error, maybe someone could help me. I have created a new type to store rgb color values. That type looks like: typedef struct pixel { unsigned char r; unsigned char…
Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74
3
votes
1 answer

How is memory handled once touched for the first time in numpy.zeros?

I recently saw that when creating a numpy array via np.empty or np.zeros, the memory of that numpy array is not actually allocated by the operating system as discussed in this answer (and this question), because numpy utilizes calloc to allocate the…
mutableVoid
  • 1,284
  • 2
  • 11
  • 29
3
votes
2 answers

How to avoid 'possibly lost' memory when using calloc to allocate memory for char array in C

I'm hoping to avoid any memory leaks in a program that needs to read a command-line argument that is a path to a parameter file to be read. If I compile the following program and check the executable with valgrind, I still get returned a leak…
p-robot
  • 4,652
  • 2
  • 29
  • 38
3
votes
2 answers

Why does calloc allocate 1 byte if nelem or elsize == zero?

I am working to develop debug implementations of the four basic memory allocation routines malloc, realloc, calloc, and free (similar in operation to Electric Fence) to debug heap corruption on embedded systems which do not have the resources to run…
Vinny
  • 154
  • 5
3
votes
1 answer

c++ pass pointer address to function

I want to change array values inside a function when pass the pointer address to this function. When I try to write to the array I receive a runtime error: Exception thrown at 0x002D1D65 in interviews.exe: 0xC0000005: Access violation writing…
Roy Ancri
  • 119
  • 2
  • 14
3
votes
2 answers

How to use calloc() in C?

Shouldn't I get an error if my string goes over 9 characters long in this program? // CString.c // 2.22.11 #include #include #include main() { char *aString = calloc(10, sizeof(char)); if (aString == NULL) …
nambvarun
  • 1,201
  • 4
  • 13
  • 14
3
votes
1 answer

How to implement calloc

I'm trying to rewrite malloc and calloc, my question is about the implementation of calloc, not how to use it. One should always use calloc() instead of malloc()+memset(), because it could take advantage of copy-on-write (COW). Some calloc's are…
Bilow
  • 2,194
  • 1
  • 19
  • 34
3
votes
2 answers

If I say calloc(1000, 23), does the 23 "round up" to 24? Or to 32?

I was wondering, do most implementations of calloc treat the size as an alignment too, and round it up to the next supported granularity? If so, then do they round up to the next power of 2, or do they round to the next multiple of 8 or 16? If…
user541686
  • 205,094
  • 128
  • 528
  • 886
3
votes
1 answer

Realloc Not Copying Old Data

Background: I created an array using calloc(), and everything was working great. Then I used realloc() to make the array larger. It seems to just create a new pointer with nothing in it and calling a runtime error when I try to access elements in…
Joel
  • 1,585
  • 2
  • 10
  • 20
3
votes
1 answer

Calloc does not initialize entire memory block to zero

While playing with the implementation of a hashmap toy example (for fun) I've found a strange behaviour, calloc does not initialize the entire memory block I want to zero, as supposed to do. The following code should produce no output if the entire…
joanlofe
  • 3,360
  • 2
  • 26
  • 44