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

Getting segmentation fault:11 in C. Why?

The following code is a solution to a problem of finding amicable pairs between input numbers. I don't yet know if the algorithm is the best it can be, but what my problem is exactly is that the code below keeps on returning segmentation fault:11. I…
Kinga
  • 1
-2
votes
1 answer

Problems with heap, PC freezes

I'm dealing with a dynamically allocated matrix. In particular I have this code (in C): int i, n, m; char **matrix; matrix = (char **)calloc((n, sizeof(char *)); for (i = 0; i <= n; ++i) { matrix[i] = (char *)calloc(m, sizeof(char)) } If 'n' is…
LordFenerSSJ
  • 195
  • 1
  • 2
  • 10
-3
votes
1 answer

Why is calloc function not allocating the array?

I'm trying to read a file and fill an array with all the characters in the file. The problem is that in the while cycle the execution stops and there is a segmentation fault error. This is the interested function: void allocAndFillArray(char…
PasqualeC
  • 3
  • 3
-3
votes
2 answers

How should I release a char pointer by passing a const char pointer for free?

I have the code below in a main.c file: #include #include #include struct mystruct {const char *name; int x;}; char *test_calloc_char_p(const char *char_p) { return (char *) calloc(1, (strlen(char_p) + 1) *…
PerduGames
  • 1,108
  • 8
  • 19
-3
votes
2 answers

Bizarre bug when trying to allocate memory in a vector in C

Well, I'm doing a "binary converter" in C, and then making a binary calculator as part of a college challenge. I worked out the algorithm the same way we do it by hand, but bizarrely, it can only convert up to 127, when I try to convert 128, I get…
motorola
  • 121
  • 5
-3
votes
1 answer

Create a dynamic array and initialize it

for a school project, we have to create a maze in c, I'm a beginner in that language, so I'm stuck at the very beginning: creating an array dynamically... I read about malloc and calloc, and tried to use it but with no success... here is my code: /*…
D Sam
  • 77
  • 1
  • 2
  • 11
-3
votes
2 answers

Calloc() not assigning zeros

I want to create a struct which encapsulates a dynamically allocated array. It looks like this: typedef struct IntArray { int *field; size_t length; } IntArray; Then, I have a function which creates such an IntArray struct: IntArray…
de_dust
  • 291
  • 5
  • 13
-3
votes
1 answer

realloc removing data already in array in C

For some reason when I reallocate the size of the array created using calloc it deletes the values that have already been inputted, maybe something else is happening but i don't understand why. I have changed the code so that it includes everything…
james
  • 101
  • 1
  • 8
-3
votes
2 answers

If I am using malloc and calloc for a float variable and assign it to int then what will happen to the rest of the 2 bytes?

int x=(int)malloc(sizeof(float)); or int y=(int)calloc(1,sizeof(double)); So in short, if I ask for a greater block of memory using malloc() or calloc() but assign it to a variable of smaller memory then what happens to the extra memory?
Strinzy
  • 41
  • 2
  • 7
-3
votes
2 answers

calloc and non contiguous memory blocks and void pointer

calloc function used to reserve memory and gives starting address of memory block but it is said that it may not allocate in contiguous address space and rather it my allocate different different non contiguous blocks but starting address we get as…
-3
votes
2 answers

why does strcpy copies more character to the variable than it is supposed to?

I am currently working on with lots of strcpy' and calloc's. And then I heard that strncpy is safer to use. So what I did was create a function that will handle strcpy.. It is shown below. void safeStrncpy(char * dest, char * src){ //copy string…
user3714598
  • 1,733
  • 5
  • 28
  • 45
-3
votes
1 answer

Dynamic Memory Allocation to a Struct's array. Program Closing [C]

I checked Google but I cannot find any solution. I'm making a program and I need to use dynamic memory allocation. This is the struct I use struct profile { char *item; int lala; char *lolo; } members[]; I want to allocate memory for…
fx773d
  • 601
  • 1
  • 5
  • 3
-4
votes
1 answer

Stack Smashing when returning pointer to array

my function neldermead looks like this: double * neldermead (double data[], double (*function)(int, double, double, double, double, double), double ia1, double ia2, double ia3, double ia4, double ia5, double rad, int k) { ... printf("8\n"); double…
BostonBrooks
  • 105
  • 1
  • 6
-4
votes
2 answers

invalid conversion from void* to strcut* in c++

when i try to use calloc for continuous memory allocation it gives me error like.. invalid conversion from void* to slotstruct(*)[100][1500] in c++ here is my code : typedef struct { int id; bool used; }slotstruct; int main(){ …
Nilam Naghor
  • 57
  • 3
  • 12
-6
votes
4 answers

Allocating memory through calloc in C

I am simply testing what would be the output if I try to dereference a pointer, which points to out of range of dynamically created memory using calloc() and expecting memory fault or some garbage value instead. It is giving 0 as output, which is…
theartist33
  • 470
  • 1
  • 6
  • 13
1 2 3
37
38