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
3
votes
1 answer

Not freeing memory in a C array

This C code (compiled as C++) is not freeing memory. The program starts out with 992kB on the 'new' line, then after allocating memory, it goes to 10MB. After freeing the memory, it only goes down to 3MB. Even doing a delete[] doesn't erase the…
JeffR
  • 765
  • 2
  • 8
  • 23
3
votes
2 answers

Some calls to "calloc" are suspiciously fast

I'm benchmarking with “Perf” (Linux, gcc). When allocating memory: point_1 = calloc (100000000, 16); //this takes nearly 1 second and perf find 27M transfers from RAM->CACHE and 1M from CACHE->RAM This is OK. But when trying to allocate two…
3
votes
2 answers

A Simple Object System

I'm working my way through the learn c the hard way book and have run into a few issues on Exercise 19. The author said that ex19 was intended for the learners to get to know the macro in c. I have no problem in understanding the concept of that,…
linuxfish
  • 89
  • 1
  • 8
3
votes
2 answers

Align the start of an array in dynamic memory in C

Possible Duplicate: Aligned memory management? I have an array which I am declaring like this int * myarray; int num_of_element; myarry = (int*) calloc(num_of_elements, sizeof(int)); The size of an int is 4 bytes however I want to ensure that…
Markus
  • 589
  • 1
  • 6
  • 19
3
votes
1 answer

Why does calloc fail to allocate 1GB on a system with 4GB of RAM?

I have a call to calloc for 1 element of just over 1 gigabyte. This call returns NULL, and checking errno reveals an insufficient memory error. However, during testing I have almost 4 gigabytes of free RAM, not to mention available virtual memory.…
taz
  • 1,506
  • 3
  • 15
  • 26
2
votes
1 answer

how can i know if calloc fails to initialize

I have read that calloc (malloc+init) will sometimes fail to init array with zero bytes (but will still return pointer to a malloc'ed array). but in documentation it does not specify that it will return NULL, is there a way to be sure that array was…
sam ray
  • 257
  • 1
  • 4
  • 11
2
votes
7 answers

malloc and calloc

I know this question may be marked as a duplicate of difference between malloc and calloc but still i would like to ask it. i know calloc initilizes the memory block,here my question is not focusussing on that part. my question is the definition of…
haris
  • 2,003
  • 4
  • 25
  • 24
2
votes
7 answers

When should you free memory dynamically allocated?

Essentially, I have created a piece of Code which consists of a tree, whereby each tree node has its own linked list containing data, (each treeNode containing data as well). So that each treeNode can have multiple data items for that specific…
PnP
  • 3,133
  • 17
  • 62
  • 95
2
votes
3 answers

Repeated calloc over the same pointer

What happens when I use different succesive calloc functions over the same pointer? int *ptr; ptr = (int *) calloc(X, sizeof(int)); ptr = (int *) calloc(Y, sizeof(int)); ptr = (int *) calloc(Z, sizeof(int)); Where X,Y,Z are three distinct values.
andreihondrari
  • 5,743
  • 5
  • 30
  • 59
2
votes
2 answers

How to advance pointer in order to shorten a string?

I am creating a char array (representing a string) and adding characters to the array from the end of the array. But I don't know how many characters I will be adding in total ahead of time. Once I have finished adding all the characters I need, I…
dayuloli
  • 16,205
  • 16
  • 71
  • 126
2
votes
2 answers

CS50 pset 4 smiley - what's the meaning of code line from license taks?

RGBTRIPLE (*image)[width] = calloc(height, width * sizeof(RGBTRIPLE)) I don't fully understand the code. What I understand is that: calloc(height, width * sizeof(RGBTRIPLE)) - we are organizing a place somewhere in heap memory of a certain size for…
2
votes
1 answer

Why am I getting errors freeing memory allocated by apriltags image_u8_create() and stored in 2D vector of pointers in C++?

I am attempting to detect apriltags in image streams. Because the images come in from multiple sources at a high rate, detecting the tags in the image callback will take too long, causing images to be dropped due to missed callbacks. I have decided…
2
votes
2 answers

Value assignment using calloc

#include #include #include int main() { int size = 5 ; int* c = (int*)calloc(size,sizeof(int)); memset(c,0,size*sizeof(int)); // when I input 1 or another value it doesn't work for (int i=0;…
Paroz
  • 39
  • 4
2
votes
2 answers

using function malloc() and free(), problems with initialized pointer

I'm not that new to coding but I faced a problem I couldn't explain to myself. #include #include int main() { char *test; test = (char *)calloc(15, sizeof(char)); // test = "Wurstbrot"; free(test); } If I…
2
votes
2 answers

Does the order of arguments matter for these library functions?

Consider these library functions: void *calloc(size_t num, size_t size); size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream); My question here is if I can safely switch some arguments. Is this: calloc(1, 8); fwrite(p, 1, 8,…
klutt
  • 30,332
  • 17
  • 55
  • 95