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

Does a realloc after a calloc zero out bytes?

I have been reading "How to realloc some memory allocated using calloc?". Now I am wondering whether a realloc followed by a calloc will zero out new bytes if the block is larger. Silly example: #include #include int test()…
Julius
  • 1,155
  • 9
  • 19
1
vote
1 answer

How do you easily differentiate the use of * as a pointer, * as a de-reference operator, * as a multiplication operator in C?

The use of * is all so confusing especially for us new comers to C. I mean, how do you easily differentiate the use of * as a pointer, * as a de-reference operator, * as a multiplication operator? I see alot of different use of * online and it's…
pasignature
  • 577
  • 1
  • 6
  • 13
1
vote
2 answers

How to allocate memory for a structure member properly?

Question's pretty self-explanatory. I want to implement a function which creates a structure with dynamic array of initial capacity of.. initial_capacity and some properties inside. Here's the code: #include #include #include…
altbrace
  • 15
  • 7
1
vote
1 answer

Calloc providing a pointer that begins inside already allocated memory?

I'm getting some kind of pointer collision, Basically, in one function I do, a = calloc(1,28); // gives me 0x100100d10 Then pretty soon in a subfunction I do, b = calloc(1,16); // gives me 0x100100d20; first address + 28 is 0x0..d2C, ie, extends…
Fettle
  • 11
  • 1
1
vote
2 answers

Why is it mandatory to explicitly typecast malloc and calloc in C++?

I recently read an article which said it's not necessary in C to explicitly typecast malloc and calloc but in C++ it is mandatory. Why is it so? Can anyone explain?
Paula
  • 11
  • 2
1
vote
0 answers

initialize array with 0, to avoid storing garbage data in 2D array, C

I want to create a raster dataset by calculating the average value of every pixel, for 14 images. The images are 10980 * 10980, and they are 14 in total. The main program is: //Main program int main() { char path[100]; char suffix[100]; …
Roger Almengor
  • 442
  • 5
  • 16
1
vote
2 answers

Should I check for NULL pointer after malloc and calloc of 2D array in C?

OK, I acknowledge that I should check for NULL pointer when mallocing, but what about calloc? Are there any memory leaks possible? int **initializeMatrix(int *rows, int *cols) { int **matrix = malloc((*rows) * sizeof(int*)); …
1
vote
1 answer

SegFault when setting up 2D character array in C

I am trying to set up a 10x10 grid filled with '$' characters and then print it. char **plot; plot = (char**)calloc(100, sizeof(char)); int i,j; for (i=0; i< 10; i++) { for(j=0; j<10; j++){ …
Thomas
  • 43
  • 6
1
vote
2 answers

Is there a way to allocate memory in C and initialize during allocation process to value other than 0?

I wonder if there is a way to initialize a string array to a value I decide on during memory allocation, so it won't contain any garbage and the null character will be placed at the correct place. I know that using calloc the memory allocated is…
liooshine
  • 105
  • 6
1
vote
1 answer

Generating a matrix with malloc and calloc causing confusing behavior

I've been working on a little project that deals with matrix computations in C. I was doing some testing against the code I wrote and came across some incredibly confusing behaviour. Before I get into the question, here is some of the relevant…
J Dub
  • 35
  • 5
1
vote
1 answer

'calloc' could not allocate memory with stat_smooth function

I use the stat_smooth() function in ggplot to graph large data sets. It works fine until I have more than 100,000 rows. Then it returns the error: 'Calloc' could not allocate memory (18446744073673801728 of 8 bytes) I am working on a server with 48…
Jordan
  • 31
  • 3
1
vote
2 answers

How to realloc some memory allocated using calloc?

I've allocated a string with the calloc function: //string1 and string2 previously declared char *stringClone = calloc(strlen(string1) + 1, sizeof(char)); Now I want to do the same thing on stringClone with a different string. Doing: stringClone =…
Carmelo Sarta
  • 81
  • 1
  • 5
1
vote
3 answers

C++ programming, dynamical memory is not working properly using malloc and calloc

I have just started learning C++ and I came on a problem I couldn't find on the Internet so I hope you can help me with. This is my code: int* a; int* b; a = (int*)calloc(1, sizeof(int)); b = (int*)calloc(5, sizeof(int)); cout << sizeof(a) <<…
Amaterastis
  • 477
  • 3
  • 12
1
vote
5 answers

What happens if I set a value outside of the memory allocated with calloc?

Consider the following: int* x = calloc(3,sizeof(int)); x[3] = 100; which is located inside of a function. I get no error when I compile and run the program, but when I run it with valgrind I get an "Invalid write of size 4". I understand that I…
Stupid
  • 133
  • 3
1
vote
1 answer

Program crashes at times while using free on a pointer which holds conditionally allocated memory

This is a simple code just to illustrate the issue I am dealing with. If you give the input of n = 3 and enter the array elements as 1, 2 ,3 and m= 0 the program crashes!!! Why is it so?? The problem occurs due to the last 2 lines involving the…
user11035496