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
1
vote
1 answer

Program does not output the complete string

I'm learning C and I have encountered a problem when printing out a string which contents I generated randomly. The code below prints out only the first 89 characters and I need to get the whole 1000. I have tried searching for similar questions…
isaric
  • 195
  • 3
  • 18
1
vote
2 answers

Understand Allocation of matrix c

I have the following function: float **alloc_float_matrix(int n) { int i; float **m = (float**)calloc(n, sizeof(float*)); for (i = 0; i < n; i++) { m[i] = (float*)calloc(n, sizeof(float)); } return m; } I want to find…
MSD561
  • 512
  • 5
  • 16
1
vote
1 answer

Malloc & calloc: different memory size allocated

So, I have this piece of code: #include #include int main() { char *p; long n = 1; while(1) { p = malloc(n * sizeof(char)); //p = calloc(n, sizeof(char)); if(p) { printf("[%ld]…
Adrian Pop
  • 1,879
  • 5
  • 28
  • 40
1
vote
1 answer

How to build program without cast result of calloc in Xcode?

I'm trying to use calloc in my program. With an explicit cast it compiles and runs fine, but when I try to remove the explicit cast of malloc I see the following error: Assigning to '... *' (aka '....... *') from incompatible type 'void *' How can…
Mohan
  • 1,871
  • 21
  • 34
1
vote
1 answer

Correct interpretation of dynamic 2D array allocation in C

I have problem to correctly interpret two different ways of dynamically allocation 2D arrays in C. This first method reads (for readability, I left out the if(arr==NULL) checks): double** matrix_d( long int Nrows, long int Ncols ) { long int …
Alf
  • 1,821
  • 3
  • 30
  • 48
1
vote
1 answer

Access violation writing location 0x00000000. When increase size of array to calloc

I try to calloc 2d array and initial value of edge cell with 255, it work correctly but when I try to set dimension of array over than 12000*12000, VS2010 show Access violation writing location 0x00000000 my function of calloc 2d array int…
ThSorn
  • 507
  • 4
  • 7
1
vote
1 answer

Call to calloc fails on 64 Bit system for arrays which are greater than 4GiB

Note: I am new to learning C and this might have a completely easy solution which I am ignorant of. In that case, please enlighten me SO. I am having issues debugging a C program, which I received from a colleague. In essence, I managed to trace the…
1
vote
3 answers

How does realloc work on memory allocated using calloc?

When we try to resize the memory allocated by malloc using realloc, we typically do this: char *ptr = (char *)malloc(size_1); ptr = (char *)realloc(ptr, size_2); If size_2 may be larger or smaller than size_1. If new size is larger then the old…
Divya
  • 393
  • 2
  • 5
  • 17
1
vote
3 answers

Segmentation fault in 2D array unsigned char using calloc in C

I've trying to allocate an unsigned char** using calloc: newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char)); for (j=0 ; j < width; j++) { if (newmatriz[j]=(unsigned char*)calloc(witdh, sizeof(unsigned char)) == NULL){ …
user3055867
  • 17
  • 1
  • 8
1
vote
1 answer

calloc created array is not acting as expected

I was bored and wanted to make a program to crash my computer :P. I would have it uselessly and redundantly allocate memory until the crash. The code I created so far is here: #include #include int main(int argc, const char…
James Parsons
  • 6,097
  • 12
  • 68
  • 108
1
vote
2 answers

Initialize dynamically allocated structures within dynamically allocated structures

I want to dynamically allocate structures within structures. I think I can do this. However when I want to perform operations with them, I get an access violation error under Visual C++. Perhaps there is something wrong with the initialization. By…
Zoltán Csáti
  • 679
  • 5
  • 17
1
vote
5 answers

Calloc a Two-Dimensional Array

To make a two dimensional array, I'm currently using the following: int * own; own = (int *)calloc(mem_size, sizeof(int)); for (i=0;i
db2791
  • 1,040
  • 6
  • 17
  • 30
1
vote
2 answers

Make calloc opportunistic

On linux malloc behaves opportunistically, only backing virtual memory by real memory when it is first accessed. Would it be possible to modify calloc so that it also behaves this way (allocating and zeroing pages when they are first accessed)?
chew socks
  • 1,406
  • 2
  • 17
  • 37
1
vote
1 answer

typedef pointer using fgets to input data, but takes last fgets

I'm trying to read postal codes from a file into an Object * array. file includes 123 Anywhere kirkland CA 99223 my .h file looks like typedef struct { char *street; char *city; char *state; int zip; }Address; my filling the…
Bob
  • 43
  • 1
  • 1
  • 7
1
vote
2 answers

calloc returns a pointer to itself

I have problems when allocating memory on a Tiva C launchpad (ARM Cortex M4), what I am trying to do is to dynamically allocate a pointer to pointers to structure inside another structure, at some point calloc() returns a pointer which is the same…
Mite
  • 57
  • 1
  • 9