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
6 answers

In C, are the characters in an array (i.e. string) stored in individual registers or are there four characters per register?

I am writing a program in C (32 bit) where I output a string (15 to 40 characters long). I have elected to use pointers and calloc instead of a formal array declaration. My program functions totally fine so this isn't a question about logic or…
MCHatora
  • 85
  • 8
2
votes
3 answers

How to malloc "MyDef ** t" to a specific length, instead of "MyDef * t[5]" in C

A struct like the following works fine, I can use t after calling malloc(sizeof(mystruct)): struct mystruct { MyDef *t[5]; }; I want to be able to dynamically set the length of the array of MyDef, like the following: struct mystruct { MyDef **t; …
2
votes
2 answers

How to initialise a pointer to pointer struct in C?

I have a struct which is a node, and another which is a list of these nodes. In the list struct, its an array of nodes, but instead of an array, it's a pointer to pointer with a size integer: typedef struct node { struct node *next; MyDef…
Igor K
  • 95
  • 1
  • 2
  • 9
2
votes
2 answers

How to determine the size of (all the content) a file so I can allocate memory for it at once?

I am trying to allocate memory for the content of a file with words(separated by: \n). How do I replace the 16000 to make it usable with files of greater size? My code: typedef struct node { bool is_word; struct node* children[27]; }…
Ranj
  • 718
  • 1
  • 12
  • 19
2
votes
3 answers

Creating an array of strings using malloc in C

I am completely novice in C, and just learned about the dynamic memory allocation using malloc, realloc, calloc and free. I want to make a small programm which takes an int number as the number of the strings that will be given and then "scanf" them…
manosgior
  • 49
  • 1
  • 6
2
votes
3 answers

malloc doesn't look to work in my program

I have a function which prints the whole content of the file and the function seems to work fine, but valgring complains about Conditional jump or move depends on uninitialised value(s) and Uninitialised value was created by a heap…
Michi
  • 5,175
  • 7
  • 33
  • 58
2
votes
2 answers

Who zeroes pages while calling calloc() in Linux?

I am aware that an implementer has a choice of whether he wants to zero a malloc page or let OS give him a zeroed page (for more optimization purposes). My question is simple - in Ubuntu 14.04 LTS which comes with linux kernel 3.16 and gcc 4.8.4,…
Prem Anand
  • 97
  • 8
2
votes
2 answers

C dynamic memory allocation

I am learning C but I am still a noob. I am writing a program as an exercise on dynamic memory allocation that takes text from the user with unknown length and gives back this text with no spaces, tabs, special characters or numbers. The program…
Nora Amer
  • 43
  • 5
2
votes
2 answers

Store argv to an int array

I have code like this to store argv to a dynamically allocated int array: int *data; // pointer to array of integer numbers int size; // size of data array int main(int argc, char* argv[]) { // initialize array data size=argc; …
necroface
  • 3,365
  • 10
  • 46
  • 70
2
votes
0 answers

Segmentation fault with calloc

I saw a lot forum with this ask, but in all answers it was because they don't verify the returned pointer. But in my case, I'm working on embedded system (so no Valgrind or Gdb), and I debug with printf… So there where my program make a segfault…
2
votes
1 answer

Free calloc in shared library using ctypes

I have this struct: struct result { int position; int *taken; }; struct result get_result(int pos, int take[]){ struct result res; res.position = pos; res.taken = take; return res; } Which I call in a function: struct…
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
2
votes
2 answers

Why does calloc always returns NULL when I pass a "big" size?

This is my code (it is and must be pure C): unsigned long buffSize = 65536; /* 64 KB */ char *block; block = calloc(1, buffSize); if (block == NULL) { /* This is always triggered */ } I want a 64 KB of zeroed memory that I will use in a loop…
AlexV
  • 22,658
  • 18
  • 85
  • 122
2
votes
3 answers

calloc() usage and checking for failure in C

G'day! Usually if I was using malloc, I'd check for failure via: int *A; A=(int *)malloc(NUM_ELEMENTS*sizeof(int)); if (!A) { printf("mem failure, exiting \n"); exit(EXIT_FAILURE); } Can I do the same thing for calloc, even though…
James Adams
  • 678
  • 4
  • 11
  • 21
2
votes
1 answer

Using calloc() to set up char array, also "freeing" array when done

I'm trying to set up an array of strings (in C, using Linux). The array will hold 11 strings (static length). I initially had the array set up as: char Answers[10][100]; but in my code I have a portion that calls fgets(input,sizeof(input),stdin).…
Katarn
  • 21
  • 1
  • 1
  • 3
2
votes
1 answer

Malloc/calloc allocates an already allocated address

I've been trying to debug this for hours. I have a struct for a scheduler. typedef struct rr_scheduler { unsigned int time_q; unsigned int avg_wait; unsigned int avg_turnaround; unsigned int processes_served; unsigned int t; …
scradam
  • 23
  • 4