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

How to avoid overflow in realloc?

One can safely allocate x elements of size y in C by using calloc(x, y) and calloc() will take care of the multiplication x*y. However realloc() for example only takes the new size as parameter and I was wondering how I could safely realloc x*y…
user194192
  • 21
  • 3
1
vote
2 answers

Calloced memory appears to be NULL

What are the possible cases that can make the following code to execute the if condition in the following snippet? As far as I'm concerned, I can't relate any cause for the if statement to execute. #include #include void…
Ahmed
  • 147
  • 7
1
vote
1 answer

strange thing about calloc

I was writing some code and I used the function calloc. I understand that, when the first and the second arguments passed to this function are both zero, the function is going to alloc the necessary space for 0 elements, each of them with size 0,…
marcosk
  • 13
  • 4
1
vote
1 answer

sorting program using pointers

I have a program that: given a sequence of numbers, sorts even numbers in ascending order, and sort odd numbers in descending order, and adds the sorted even numbers to an array followed by the sorted odd numbers. Example: Input: 1, 2, 3, 4, 5,…
1
vote
0 answers

Segmentation fault using calloc() in C

I've ran into a bit of a pickle when using malloc or calloc. I keep getting a seg fault, I tested for it's allocation of memory, I free the memory afterwards. The program is an exercise in resizing an BMP image. I've been able to successfully copy…
codeinaire
  • 1,682
  • 1
  • 13
  • 26
1
vote
1 answer

How to check if malloc() overcommits memory

In my C program, based on the user's input, memory will be allocated for a given simulation. The initial problem I faced is that user can ask for a huge number to allocate but malloc() never fails until it runs out of memory then the program…
Pourya
  • 21
  • 4
1
vote
3 answers

how to allocate memory for 3D array using calloc in c++

I want to allocate memory for 3d array in c++ one by one like.. typedef struct { int id;int use; }slotstruct; slotstruct slot1[3][100][1500]; // This should be 3d array for(i=0;i<3;i++){ for(j=0;j<100;j++){ for(k=0;k<1500;k++){ …
Nilam Naghor
  • 57
  • 3
  • 12
1
vote
1 answer

calloc overwrites another variable's memory?

I am writing a program to create a gray scale image (image) using calculations on data stored in source array (hist). The data stored in source array resets to zero after calling calloc for image. func1(){ float * hist = (float *) calloc(256,…
CuriousCat
  • 429
  • 5
  • 16
1
vote
2 answers

how to put a parsed string inside of malloc/calloc/dynamic memory?

So I'm doing a few practice questions for a final exam coming up. and I'm having a lot of trouble with dynamic memory. So the question wants to basically parse through 2 different sources and compare them to find the similar words. (one from a csv…
harekuin
  • 45
  • 1
  • 8
1
vote
1 answer

Why does not free() deallocate all allocated memory locations?

I don't know if I am doing something wrong or if my concept is somewhat wrong #include #include int main() { int *p; p=calloc(3,sizeof(int)); p[0]=10; p[1]=15; p[2]=30; …
1
vote
1 answer

When I 0-Initialize a vector Does It Have the Same Effect as calloc?

So calloc calls on the OS to retrieve zeroed pages on the heap: https://stackoverflow.com/a/2688522/2642059 What about C++11's vector constructor that only takes a size_t and 0-initializes the values? Ask the OS for a zeroed page in the general…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
1 answer

valgrind memcheck conditional jump traces it back to brk and sbrk functions

I have been looking for a similar post to mine without success. I am working with a code that so far seems to be deterministic and is as of now working properly. However when running Valgrind memcheck on it I get thousands of warnings all…
1
vote
2 answers

How to create an array within a constructor in Rust

I want to migrate some code from C to Rust for learning purposes and to make my learning library a bit more multi-lingual. The problem is that I know there's a way to integrate C libraries into Rust. That way I could use calloc in Rust to allow…
xetra11
  • 7,671
  • 14
  • 84
  • 159
1
vote
1 answer

corrupted double-linked list on free()

I successfully run my code in small data but when i tried large scaled data, it gives me "corrupted double-linked list:" In small data, if I delete free() functions, error is disappeared and code works, but in large data whether i put free or…
abby
  • 27
  • 1
  • 6
1
vote
2 answers

invalid application of ‘sizeof’ and compilation error with struct data

I created a data type which I want to send over a socket. I'm getting a compilation error and a segmentation fault error. The compilation error I get is error: invalid application of ‘sizeof’ to incomplete type ‘struct udp_msg_t’ whereas the…
Pheonix7
  • 2,131
  • 5
  • 21
  • 38