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

What's the difference between calloc & malloc followed by a memset?

Possible Duplicate: c difference between malloc and calloc why malloc+memset slower than calloc? What's the difference between calloc & malloc followed by a memset? If I replace all calls to calloc with a malloc followed by a memset, will it be…
Jay
  • 24,173
  • 25
  • 93
  • 141
2
votes
1 answer

C Dynamic Memory Allocation - Read Data from File

I am working on replicating the load() function from MATLAB for use in a C application. I am having trouble dynamically loading the data and initializing the arrays that I need. More specifically, I am trying to use fgets with arrays that have been…
Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72
1
vote
5 answers

alloc storage and binary fwrite shorts in c

I am trying to allocate a single block of shorts, fwrite it to a file, and then read it back. But the data that gets written into the file doesn't match what is coming out. I've isolated the problem to the following bit of code. Any…
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57
1
vote
1 answer

explaination of glibc calloc implementation i.e public_cALLOc( )

in glibc malloc.c for calloc() (precisely, public_cALLOc()) implementation, when it tries to 0 out the memory it is doing in two ways, if the number of bytes are greater than 36 then straight away memset() is called otherwise it put 0 bytewise…
Kapil
  • 836
  • 2
  • 8
  • 20
1
vote
2 answers

What type can the result of calloc be assigned to, a pointer to an array, a pointer to the type contained within the array, or either?

According to the standard (C17 draft, 7.22.3.2), calloc void *calloc(size_t nmemb, size_t size); "allocates space for an array of nmemb objects, each of whose size is size" (and initializes all bits to zero). For calloced arrays of T, I have only…
Lover of Structure
  • 1,561
  • 3
  • 11
  • 27
1
vote
1 answer

List in C Error - Stack around the variable 'memory' was corrupted

I am trying to implement a dynamic List in C and I do not get why I get the "Stack around the variable 'memory' was corrupted." Error when I am trying to add a second Item to the list. struct LIST { unsigned int size; unsigned int count; …
1
vote
3 answers

Can you dynamically initialize stack memory to zero?

I have an integer array that's only useful in a single code block. I'd like to declare it and initialise it to zero on the stack to avoid the overhead of the heap. I'd like to do this dynamically and initialise every index to zero. Basically, I'd…
Connor
  • 867
  • 7
  • 18
1
vote
1 answer

How do I read a large file of numbers and store its content in an array?

My original task is: Given a file of numbers, I have to find all pairs (a pair is just 2 numbers; they cannot be consecutive) that fulfil a certain condition. To do that, I've decided to create an array where I will store all the numbers. I have a…
Rodion Iskhakov
  • 139
  • 1
  • 9
1
vote
2 answers

C - repeat a string for a specific number of times in anothervariable

I want to repeat a string - for example hello - for a specific number of imes - for example 3 times -, but it doesnt work :) The example should look like this: hellohellohello, but I get no output or i get HHHHHHHHHHH... here is my code: char…
programmmm
  • 19
  • 6
1
vote
2 answers

Malloc is throwing an error for pointer being freed

so I have a function that creates a flexible array type and all paths use calloc to define the size of the array. However when I try to destroy the struct, it says that the memory was not allocated. typedef struct { int size; void *items; }…
cobb208
  • 55
  • 1
  • 7
1
vote
1 answer

Calloc causes segmantation fault

I am trying to add a set a string in my struct array's struct. I coded this in Macos using gcc (not clang) and works fine but when i import my code to Windows this calloc causes segmantation fault. index =…
1
vote
3 answers

Allocate memory for big .txt file in C

I need to allocate memory using malloc or calloc, for a large file that looks like this: 2357 VKLYKK 7947 1WTFWZ 3102 F2IXK3 2963 EXMW55 2865 50CJES 2510 8PC1AI There are around 10K of lines in that .txt file. How can I allocate the required…
Astaroth
  • 19
  • 4
1
vote
1 answer

I keep getting "ERROR: AddressSanitizer: heap-buffer-overflow on address"

I can't figure out this error. Problem > Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". char * longestCommonPrefix(char ** strs, int strsSize){ int…
Hans
  • 49
  • 1
  • 7
1
vote
0 answers

Undefined reference to standard library functions in plib

xc32 v2.41 or older working ok with plibs but the latest version of xc32 #include int main() { } generating this error : "C:\Program Files\Microchip\xc32\v4.00\bin\xc32-gcc.exe" -mprocessor=32MX664F128L -o…
voidpointer
  • 301
  • 1
  • 8
1
vote
2 answers

Why am I getting error although everything is declared well?

#include #include int main() { int *ptr; int n; printf("Enter the no of elements you want in an array1:"); scanf("%d",&n); printf("The no of elements in array 1 would be %d",n); …
Noob_coder
  • 21
  • 1
  • 5