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

Error in `./a.out': free(): invalid next size (normal) while freeing dynamically allocated 2D array of struct

Basically, I am creating 2D array of struct using calloc(). Then I utilize that array and I free that allocated space, while freeing it I am getting "double free or corruption (!prev)". Code is written in C language. Code: #include…
hp2304
  • 13
  • 4
1
vote
2 answers

How to calloc a 2D array without valgrind errors?

I am working on a school project, trying to create a 2d array based on variables. int **wagner; wagner = (int **)calloc((sizeofvstup1 + 1), sizeof(int)); for (int i = 0; i < (sizeofvstup1 + 1); i++) { wagner[i] =(int *)calloc((sizeofvstup2 +…
1
vote
0 answers

Using calloc() to create dynamic struct utmp array

I'm attempting to create a dynamic array in C but I am a little confused on how to go about it. Firstly calloc() seems to be giving me strange results. I have some code as follows: struct utmp userRec; printf("%d\n", sizeof(userRec)); //should print…
1
vote
3 answers

Dynamic Memory storage issue after realloc - C

For an assignment at school, we have to use structs to make matrices that can store a infinite amount of points for an infinite amount of matrices. (theoretical infinite) For the assignment I decided to use calloc and realloc. How the sizes for the…
1
vote
8 answers

C best way to clear an array of doubles

In C, How can I reset a given pointer to have all values in an array be a specified value? Is it best using a for loop, or is there a function that I can use to set all values in an array to 0. My code currently is something like this: int main() { …
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
1
vote
2 answers

C - Integer array yielding different values in a for loop

I am iterating through an integer array and trying to find the elements that are non-zero and getting the count. Here is my complete code. #include #include int countelem(int *a, int a_length){ int i, count = 0; for…
SreeT
  • 21
  • 1
1
vote
2 answers

Using data stored in calloc buffer

I was wondering if someone could explain how I can use the data stored in a dynamically allocated memory buffer. My aim is to read a text file, store this in said buffer (which works). Then loop through this buffer to gather and store the desired…
S.NewUser
  • 11
  • 3
1
vote
3 answers

Recieving C socket data to allocated 2D array

I am trying to populate an array I allocated with data recieved from a socket, but am unable to make it work properly. When i define and get data through: uint16_t data[2048]; recv(network_socket, &data, sizeof(data), 0); the data is recieved…
David Montgomery
  • 473
  • 1
  • 4
  • 15
1
vote
2 answers

Difference between malloc and calloc with std::string

I have recently gotten into C++ and I've encountered a problem working with malloc. The code below does not print out "Success" (Program crashes with exit code 0xC0000005) whereas if I use calloc instead everything works fine. int main(){ …
TinSlam
  • 11
  • 6
1
vote
4 answers

How to store custom objects (struct) in C?

I want to know how to store custom objects (not their pointers) in C. I have created a custom structure called Node #define MAXQ 100 typedef struct { int state[MAXQ]; int height; } Node; (which works) and I want to store a few of these Nodes…
mehfluffy
  • 49
  • 1
  • 9
1
vote
0 answers

Malloc and Calloc not freeing correctly

Hello I seem to be having an issue with freeing some of my variables after creating them. Little background on my program. My program is reading MatrixMarket files and building a Matrix Structure from the information read in. I am then using the…
JeanP
  • 406
  • 9
  • 27
1
vote
2 answers

C access violation after using calloc

Note: C is Microsoft C Compiler. I'm having trouble with the following code. *Roomsize = (int*)calloc(sizeof(int),sched->numberOfRooms); roomIndex = 0; for(roomIndex=0; roomIndex< sched->numberOfRooms; roomIndex++) { …
RekrowYnapmoc
  • 2,166
  • 2
  • 22
  • 23
1
vote
1 answer

Error when freeing calloc'd memory: free invalid next size (fast)

I'm writing a function to get text from a file and I'm encountering an issue when I try and free some calloc'd memory. ye. wee.\n when a txt file with the above is passed, the function allocates memory for the 3 char characters of ye. and copies…
1
vote
3 answers

multi-dimension array allocation with calloc

I have the following 2-dimension array N*2 (I can't modify its declaration): bool* myArray[2]; int N; I want to allocate it with calloc but no success: myArray = calloc(N, 2*sizeof(bool)); for (int i=0; i!=N; i++) { myArray[i] = calloc(2,…
rudy
  • 176
  • 3
  • 14
1
vote
0 answers

Cray C compiler doesn't like "calloc"

This very simple code runs fine when compiled with GNU, Intel, or PGI (which is really GNU, I think?). Under Cray, it never makes it to "debug6"; it fails in the "calloc" call, returning an "Illegal instruction" error. Anyone see a problem? EDIT:…
bob.sacamento
  • 6,283
  • 10
  • 56
  • 115