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

calloc returns success when malloc fails

Is there a scenario where malloc fails, while calloc returns success. Suppose i give malloc(20) and calloc(4*5), does there exist any scenario where malloc could fail and calloc succeeds. If so what is the exact reason for this.
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
1
vote
3 answers

(C) malloc corrupted top size with random numbers

So I'm trying to get an array of random 8-bit numbers of size count but I get the corrupted top size error whenever count is larger than 6. I read that it should be related to memory assignment but I can't figure out what is wrong exactly. I'm new…
Akash
  • 13
  • 2
1
vote
2 answers

Segmentation fault: 11 while trying to print array with 50k items

I'm trying to print array with 50k items into a file but it could be done only if I set small numbers of items, e.g. 5k. void fputsArray(int *arr, int size, char *filename) { char *string = (char*)calloc( (8*size+1), sizeof(char) ); for(int…
1
vote
3 answers

Calloc behaves differently on different computers

I have written the folowing code in C: #include #include int func(int n, int * data){ for(int i = 1; i <= n; i++){ data = realloc(data, i * sizeof(int)); data[i - 1] = i; } return 0; } int main(void){ int numb…
1
vote
1 answer

Dinamically vs Static memory allocation for a struct array

If you want to allocate an array of struct you can do it statically by declaring something like struct myStruct myStructArray[100]; or dinamically with something like struct myStruct *myStructArray = calloc(100, sizeof(struct myStruct) ); but in…
Bemipefe
  • 1,397
  • 4
  • 17
  • 30
1
vote
1 answer

lazy overcommit allocation and calloc

Can someone in the know please explain how lazy-backed heap storage interacts with the memory-zeroing guarantees of calloc/realloc? Specifically, I would like to know: if/when the zero writes would cause storage to be faulted in…
l.k
  • 199
  • 8
1
vote
1 answer

Do NSThread have same memory privileges as main thread?

I'm using NSOperationQueue to manage a phase of an iOS application which is quite long so I would like to manage it asynchronously. Inside that phase I allocate big arrays in C by using directly calloc functions. With big I mean a 1024x256…
Jack
  • 131,802
  • 30
  • 241
  • 343
1
vote
1 answer

calloc problem - malloc.c:2385 error - how does calloc/malloc work?

When running my program I receive following error: malloc.c:2385: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1))…
Catelyna
  • 11
  • 1
1
vote
2 answers

Dynamic memory allocation for char**

I'm trying to dynamically allocate memory for an array of strings, but I'm suffering a segmentation fault. If you can show me some ways to do it, that would be really helpful. My knowledge so far is that char* is a string, and char** is a…
1
vote
0 answers

How to intercept and replace calls to calloc

For an extremely light version of ASAN, I would like to intercept calls to malloc, free, calloc and others. I'm doing this because for my use-case, even ASAN is too slow. My approach would be creating an LD_PRELOAD library which replaces e.g.…
Folkert van Heusden
  • 433
  • 4
  • 17
  • 38
1
vote
1 answer

How to delete elements from the middle of a pointer array using free() or any other method?

I have created a pointer array using calloc and I want to delete elements starting from the middle of the array.What are the ways in which this can be done?
1
vote
1 answer

Error while declaring ptr using calloc in global scope

[cquery] type specifier missing, defaults to 'int' [-Wimplicit-int] [cquery] redefinition of 'ptr' with a different type: 'int' vs 'int * int *ptr,size=50; ptr=(int*) calloc(size,sizeof(int)); How can i fix this error?Also what is the reason behind…
1
vote
3 answers

Allocating a pointer with calloc, and then dynamically allocate each cell with malloc = memory leakage?

In a recent exam question I got this code with following options: char **mptr, *pt1; int i; mptr = calloc(10, sizeof(char*)); for (i=0; i<10; i++) { mptr[i] = ( char *)malloc(10); } Which of the following de-allocation strategies creates a…
B.Castarunza
  • 135
  • 12
1
vote
3 answers

How to save the scanf input only if there's enough space in the array? How to reallocate array to let the scanf input fits in?

#include int main() { char *mystring = calloc(2, sizeof(char)); scanf("%10[^\n]s", mystring); printf("\nValue: %s\nSize of array: %d\nAllocated space: %d\n", mystring, 2 * sizeof(char), sizeof(char) *…
Mnkisd
  • 504
  • 2
  • 12
1
vote
3 answers

Problems in three dimension array in C

I defined the three dimensional array like this, but I it can't read any string in it? Where is the problem? Thanks! int stuTotal, courseTotal,i,k;//a dynamic array printf("Input the total number of students that you would like to…
LUCY
  • 15
  • 4