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

A problem that is about calloc and free functions in C

I'm new in C. Here is my code: int *i = (int *)calloc(10, sizeof(int)); i[0] = 3; i[1] = 1; i[2] = 2; i[3] = 5; printf("before: %d %d %d %d\n", i[0], i[1], i[2], i[3]); printf("before: %d %d\n", i, (i + 3)); free(i); printf("after: %d %d %d %d\n",…
essnape
  • 23
  • 4
2
votes
3 answers

struct c dynamically allocate memory

I am using a struct and I want to initialize a maximum of 10 ports. However, when the program is running it could be a lot less, we don't know until run-time. However, this will be the max. I have never done struct like this before, as I normally…
ant2009
  • 27,094
  • 154
  • 411
  • 609
2
votes
1 answer

Null-terminated strings created with calloc?

I want to allocate memory for a string using calloc, I know that calloc fills the whole allocated memory with 0, but I also found out that they are different from \0 in some contexts. This whole discussion it's kind of confusing for a newbie (like…
B.Castarunza
  • 135
  • 12
2
votes
1 answer

How to create a array of structs for matrixes

Basically I have matrices and want to store them using a struct but I need to do it using dynamic memory allocation typedef struct { int mymatrix[5][5]; // the matrix int column; // will hold column number int row; …
Herion
  • 35
  • 5
2
votes
0 answers

Can the memory allocation functions be inconsistent regarding zero size?

The C specification says this about the memory allocation functions: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value,…
Barmar
  • 741,623
  • 53
  • 500
  • 612
2
votes
1 answer

Is there any specific difference between calloc calls of equivalent length but different per-object size?

// Is calloc(10, 5); // exactly the same as calloc(50, 1); calloc(25, 2); etc. in all possible cases?
Chris
  • 28,822
  • 27
  • 83
  • 158
2
votes
2 answers

Reprogramming Calloc / Realloc in C Using void pointers

I'm actually learning C programming and my school actually doesn't allow us to use calloc / realloc without reprogramming them. That's why I'm asking for help. Here is my problem : I want to use void * to make my code reusable but I encounter the…
Dzious
  • 169
  • 2
  • 14
2
votes
2 answers

Initializing a void* buffer with calloc

So I've read the documentation for calloc and it says that it will initialize n objects of size size and initialize each of them to 0. So before making my implementation of a generic dynamic array in C I decided to make it with int type to make…
user8298902
2
votes
2 answers

Using Calloc to initialize elements of 2D array using C

I am wondering how to go about storing strings into an array of strings. char buff[1024]; // to get chars from fgetc char *line[2024]; // to store strings found in buff int ch; int i = 0; while ((ch = fgetc(file)) != EOF) { buff[i] = ch; if…
Kev K
  • 17
  • 2
2
votes
1 answer

Managing dynamically allocatable arrays inside a custom structure

I would like to define a custom structure that contains two dynamically allocatable integer arrays a and b. In order to allocate memory for the arrays and initialise arrays with values I have written a "constructor" function initp. My approach is…
mabalenk
  • 887
  • 1
  • 8
  • 17
2
votes
1 answer

Relation between calloc char array and null terminating character

This whole "debate" on the web about strncpy being safe vs unsafe is driving me crazy. I find some people saying strncpy is the "devil", which to me sounds like they lack the programming discipline. I get that there is no \0 character added to the…
ThatsRightJack
  • 721
  • 6
  • 29
2
votes
2 answers

Can all bits 0 be a trap representation for integers?

It is common to assume that initializing an object to all bits 0 is a simple way to set all its members to 0. The standard does not guarantee this for non integer types as: all bits zero might not be a valid representation for pointers, even null…
chqrlie
  • 131,814
  • 10
  • 121
  • 189
2
votes
3 answers

Confusion about malloc and calloc function in C

Declaration of malloc function: void *malloc(size_t size); Here, malloc returns void pointer. So, A void function returns nothing, then Why we assign malloc(function call) value to pointer? For example: int *ptr; ptr = malloc(10 * sizeof…
Jayesh
  • 4,755
  • 9
  • 32
  • 62
2
votes
1 answer

very large memory allocation in 64-bit linux

I am trying to allocate a single very large piece of memory (>2.5gb) on a centos 64-bit linux. The hardware has more than 16gb physical memory. However, when I use malloc or calloc, they return null. The code runs in root account, and as far as I…
mete
  • 589
  • 2
  • 6
  • 17
2
votes
3 answers

What will happen in this case of malloc and realloc

malloc take some contiguous part of heap for some variable, ------------- | p | ------------- again malloc happens for some other pointer ------------- ----------------- | P | S | -------------…
acidlategamer
  • 163
  • 2
  • 14