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

C Array Behaviour - Global / Local / Dynamic

I am having problems updating an array globally from a while loop, as expalined below. Please note that I can only use functionality from C 95 and before. Anyhelp would be greatly appreciated! Full paste bin http://pastebin.com/ss6VgTCD Declared at…
Henry Quekett
  • 429
  • 1
  • 7
  • 16
2
votes
2 answers

Does calloc initialize structures arrays?

I have the doubt if calloc initialize to zero all of the elements of a struct array like: #define MAXDATA 10 struct Est2 { int dato0; // Index k int dato1; // Index j int dato2; // Index i double dato3; // Y Coordinate }; Est2…
user3517524
  • 21
  • 1
  • 2
2
votes
5 answers

Calloc inside function

Looking at this question that has just been asked: Inconveniences of pointers to static variables would doing something like this be considered bad practice, then? char* strpart(char* string, int start, int count) { char* strtemp; int i =…
user257111
2
votes
4 answers

calloc and copying data into memory area using c

I'm trying to allocate a block of memory and then copy data into that space. I made this simple program and it doesn't do what I expect it to do. Could someone please point out my faulty reasoning. Thanks. #include #include…
NomadAlien
  • 9,090
  • 6
  • 25
  • 22
2
votes
6 answers

Freeing memory after use

I have a command line C program for which I use the calloc() function to assign some memory for a struct which also has a struct in it with some memory assigned. If I use the free() function to release the memory from the parent struct, will it also…
Cheetah
  • 13,785
  • 31
  • 106
  • 190
2
votes
4 answers

"Pointer being freed was not allocated" happen on mac but not on window7

I am doing an exercise on a book, changing the words in a sentence into pig latin. The code works fine in window 7, but when I compiled it in mac, the error comes out. After some testings, the error comes from there. I don't understand the reason of…
code4j
  • 4,208
  • 5
  • 34
  • 51
2
votes
1 answer

Are memory addresses reported in valgrind absolute, physical addresses?

While running an MPI program with valgrind using mpirun -np 3 valgrind test, I noticed that the addresses of malloc/calloc'ed arrays are sometimes identical for different processes. This would lead me to believe that the addresses reported by…
ladc
  • 23
  • 2
2
votes
1 answer

error: a value of type "void *" cannot be assigned to an entity of type "float *"

Any idea why is this happening here? float *image; long size_img=par->N*par->M; image = calloc(size_img, sizeof(float));//the compiler shows error here The error is error: a value of type "void *" cannot be assigned to an entity of type "float…
Atirag
  • 1,660
  • 7
  • 32
  • 60
2
votes
2 answers

Calloc(), Structs & C

So I'm new to C and creating some simple programs to help me get a hang of things. Let's say I have a struct as follows: typedef struct { char* field; } something; And I dynamically allocate space for 10 of these as follows: something* stuff =…
Paul B
  • 349
  • 4
  • 14
2
votes
2 answers

Most portable ways of allocating memory and reading arrays with fread() in C

Just wondering what is the soundest way to allocate memory and fread() array data from a file in C. First, an explanation: int32_t longBuffer; Now, when freading in the longBuffer, the code could go as: fread(&longBuffer, sizeof(longBuffer), 1,…
user1284631
  • 4,446
  • 36
  • 61
2
votes
2 answers

can calloc or malloc be used to allocate ONLY physical memory in OSX?

I am playing around with the c functions malloc and calloc and I have some questions. I want to see if I can use these 2 functions to allocate only physical memory, my mac has 4gb or ram and when I use malloc I can allocate way more than 4gb, which…
Josh
  • 692
  • 2
  • 9
  • 38
2
votes
2 answers

Calloc causes segfault but not malloc

I am implementing a ringbuffer and in one method I am reading CHUNKSIZE bytes from a file in a loop and insert the pointer into the ringbuffer. I am doing this in a while loop. The code works fine with malloc but calloc causes a segfault at the end…
muxamilian
  • 342
  • 2
  • 11
2
votes
4 answers

Failure of free()

If I'm allocating memory in a loop like so for(file = 0; file < nfile; file++){ ... ... ... for(yy = 0; yy < ngridy; yy++){ for(xx = 0; xx < ngridx; xx++) { tmparr1[xx+(ngridx*yy)] = (double *)calloc(nptperf[file],…
Kitchi
  • 1,874
  • 4
  • 28
  • 46
2
votes
2 answers

ARC is releasing calloc'ed memory?

Something strange is going on in my code. Basically im doing the network stream application that transfers some data into ring buffer memory on iOS and then afterwards read the memory. I was getting EXC_BAD_ACCESS after some undetermined amount of…
Gossamer
  • 309
  • 2
  • 16
2
votes
4 answers

Malloc, Calloc for testing the limits of my memory

I'm trying to write a c program to test how much memory is on my system. I'm planning to run it under various different conditions: With swap enabled With swap disabled and overcommit (/proc/sys/vm/overcommit_memory) set to false With swap…
Steve Walsh
  • 6,363
  • 12
  • 42
  • 54