Questions tagged [calloc]

The calloc function performs dynamic memory allocation in C, and is part of the standard library.

calloc
Filter by
Sorted by
Tagged with
2 votes
0 answers
187 views

Can the memory allocation functions be inconsistent regarding zero size?

The C specification says this about the memory allocation functions: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the ...
Barmar's user avatar
  • 764k
3 votes
2 answers
317 views

Why does calloc allocate 1 byte if nelem or elsize == zero?

I am working to develop debug implementations of the four basic memory allocation routines malloc, realloc, calloc, and free (similar in operation to Electric Fence) to debug heap corruption on ...
Vinny's user avatar
  • 175
0 votes
1 answer
283 views

calloc() to a pointer in struct doesn't work on clang [closed]

I have implemented a queue in C. Consider the below code: #include <stdio.h> #include <stdlib.h> typedef struct queue queue; struct queue { int len; int head; int tail; int* ...
Patrik Cajka's user avatar
1 vote
1 answer
97 views

Generating a matrix with malloc and calloc causing confusing behavior

I've been working on a little project that deals with matrix computations in C. I was doing some testing against the code I wrote and came across some incredibly confusing behaviour. Before I get ...
J Dub's user avatar
  • 35
0 votes
0 answers
141 views

realloc() : invalid next size making p_thread program

I am attempting to make a multithreaded program that computes the prime-factor of each input(each input having its own thread). Using realloc or calloc with a int* Name, inputing large numbers or lots ...
SuperCjp23's user avatar
2 votes
1 answer
106 views

Is there any specific difference between calloc calls of equivalent length but different per-object size? [duplicate]

// Is calloc(10, 5); // exactly the same as calloc(50, 1); calloc(25, 2); etc. in all possible cases?
Chris's user avatar
  • 30.1k
0 votes
1 answer
629 views

How to correctly allocate a large chunk of RAM with calloc/malloc in Windows 10?

I need to allocate memory for a vector with n=10^9 (1 billion) rows using calloc or malloc but when I try to allocate this amount of memory the system crashes and returns me NULL, which I presumed to ...
Iron Maiden's user avatar
3 votes
1 answer
354 views

c++ pass pointer address to function

I want to change array values inside a function when pass the pointer address to this function. When I try to write to the array I receive a runtime error: Exception thrown at 0x002D1D65 in ...
Roy Ancri's user avatar
  • 119
-3 votes
2 answers
109 views

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 ...
motorola's user avatar
  • 121
1 vote
1 answer
2k views

'calloc' could not allocate memory with stat_smooth function

I use the stat_smooth() function in ggplot to graph large data sets. It works fine until I have more than 100,000 rows. Then it returns the error: 'Calloc' could not allocate memory (...
Jordan's user avatar
  • 31
1 vote
2 answers
875 views

How to realloc some memory allocated using calloc?

I've allocated a string with the calloc function: //string1 and string2 previously declared char *stringClone = calloc(strlen(string1) + 1, sizeof(char)); Now I want to do the same thing on ...
Carmelo Sarta's user avatar
-2 votes
1 answer
72 views

How is it possible for this code(in the description) to work?

If you guys see the below code, no memory is allocated( only 0 bytes is allocated) to ptr. But how ptr[0],ptr[1].....ptr[n] can be initialized if there is no space for them. #include<stdio.h> #...
Allan's user avatar
  • 130
0 votes
2 answers
257 views

How can I resolve calloc() error in .cpp and .hpp files?

I try to run a .cpp with a .hpp file in Linux using this command: g++ -c main.cpp but I have this error about calloc(): error: there are no arguments to ‘calloc’ that depend on a template parameter, ...
Mike Bee's user avatar
0 votes
0 answers
119 views

Allocate memory for multiple pointer of struct recursively

How I can to allocate memory recursively for a struct? Currently I allocate memory for each members but is a lot of code and I lock for a better method. struct A { long *a1; bool *a2; } ...
Adrian Simionescu's user avatar
1 vote
3 answers
522 views

C++ programming, dynamical memory is not working properly using malloc and calloc

I have just started learning C++ and I came on a problem I couldn't find on the Internet so I hope you can help me with. This is my code: int* a; int* b; a = (int*)calloc(1, sizeof(int)); b = (int*)...
Amaterastis's user avatar
1 vote
5 answers
731 views

What happens if I set a value outside of the memory allocated with calloc?

Consider the following: int* x = calloc(3,sizeof(int)); x[3] = 100; which is located inside of a function. I get no error when I compile and run the program, but when I run it with valgrind I get ...
Stupid's user avatar
  • 133
0 votes
1 answer
114 views

Calloc allocation is inconsistent

I'm creating a 2d array of integers using pointers and the calloc function. The thing is that I allocate a 3x5 matrix. And indeed, there are 3 rows but 6 columns. I can't understand this behavior. ...
Moleson's user avatar
  • 169
0 votes
1 answer
36 views

realloc(): invalid next size in strtok operation

in this part of my program, I am trying to mimic the CDUP operation in ftp servers. First I get the current directory the client is at (ptr2 and buf3) and then I attempt to strip off the last string ...
JC1's user avatar
  • 869
0 votes
0 answers
49 views

Error: To get user input for a dynamically allocated 2D array

I'm trying to write a basic fixed size array of dynamically allocated array(for when we only know one dimension of a 2D array) and getting the user input for the matrix. I've learnt that after you ...
Neo's user avatar
  • 19
1 vote
1 answer
105 views

Program crashes at times while using free on a pointer which holds conditionally allocated memory

This is a simple code just to illustrate the issue I am dealing with. If you give the input of n = 3 and enter the array elements as 1, 2 ,3 and m= 0 the program crashes!!! Why is it so?? The ...
user avatar
0 votes
1 answer
151 views

problem with filling a pointer array with random numbers initialised by calloc

my program should create 10 Vectors each with a random dimension between 1 and 10 and each dimension filled with random numbers between 0 and 24. int * makeVector(int dimension) { int * Vector = ...
ProudSalsa's user avatar
1 vote
1 answer
517 views

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 ...
hp2304's user avatar
  • 13
1 vote
2 answers
100 views

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+...
Martin Indrych's user avatar
-1 votes
2 answers
497 views

Invalid write after allocating memory for struct member in array of structs

I need to pass an array of structs to a function and it is my understanding that I have to allocate memory for the whole array of structs, as well as for each individual struct member in every struct ...
ci7i2en4's user avatar
  • 834
0 votes
2 answers
123 views

I keep getting a segmentation fault and I'm not sure why

I'm pretty sure it has to do with my use of calloc() but I don't understand why. The objective of the program is for char* C to contain characters of char* A that are not in char* B. #include<...
ajwong's user avatar
  • 11
1 vote
0 answers
45 views

Using calloc() to create dynamic struct utmp array [duplicate]

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 ...
Kody Richardson's user avatar
0 votes
3 answers
2k views

The initialization of the dynamically allocated memory in C [duplicate]

I am still confused with the two functions malloc() and calloc() As we know calloc() initialize the memory it allocates while malloc() doesn't. But when I tried the following code, the result seemed ...
Liu's user avatar
  • 433
0 votes
3 answers
81 views

c - Write on a string but every k bytes

the exercise gives me a main that prints the string list this way, that is, knows that every MAX_NAME_LENGTH has a meaningful name and prints it. #define MAX_NAME_LENGTH 32 #define DIM 4 int main(...
JayJona's user avatar
  • 439
5 votes
3 answers
576 views

Why memset called after calloc?

I've researched code of some library and noticed that calls to calloc there are followed by memset for block allocated by calloc. I've found this question with quite comprehensive answer on ...
toozyfuzzy's user avatar
  • 1,108
0 votes
2 answers
158 views

Why does gets() read in more characters to the pointer than the limit I set it when initializing it with calloc()? [duplicate]

I'm trying to get a hold of dynamic memory allocation and I just want my program to get a string and the max number of characters that should be printed from the string from the user, then just output ...
Y. J.'s user avatar
  • 45
2 votes
2 answers
2k views

Reprogramming Calloc / Realloc in C Using void pointers

I'm actually learning C programming and my school actually doesn't allow us to use calloc / realloc without reprogramming them. That's why I'm asking for help. Here is my problem : I want to use ...
Dzious's user avatar
  • 169
0 votes
1 answer
402 views

Segmentation fault allocating memory with struct of struct

i am quite new using allocate memory and pointers, my idea is try to create a struct inside a struct in order to use them after in the code, however far now i obtained a segmentation fault error that ...
federico rovere's user avatar
0 votes
1 answer
267 views

calloc issue on second time - on C

bellow is the code: from some reason the calloc inside the while loop, is failing on the second iteration. it looks the heap is corupted (not sure) but not clear the root cause. please also take a ...
efrat's user avatar
  • 13
0 votes
2 answers
2k views

Initial value of calloc, when using with a struct

I am trying to write a very simple hash table by using following code: #include <stdio.h> #include <string.h> #define SIZE 10 typedef struct { char *first_name; char *last_name; }...
drmirk's user avatar
  • 453
7 votes
4 answers
2k views

Does calloc() of a double field always evaluate to 0.0?

Does calloc() of a double field always evaluate to 0.0? Furthermore: Does calloc() of a float field always evaluate to 0.0f? Does calloc() of an int or unsigned int field always evaluate to 0? ...
u17's user avatar
  • 2,796
2 votes
2 answers
1k views

Initializing a void* buffer with calloc [duplicate]

So I've read the documentation for calloc and it says that it will initialize n objects of size size and initialize each of them to 0. So before making my implementation of a generic dynamic array in ...
user avatar
2 votes
2 answers
1k views

Using Calloc to initialize elements of 2D array using C

I am wondering how to go about storing strings into an array of strings. char buff[1024]; // to get chars from fgetc char *line[2024]; // to store strings found in buff int ch; int i = 0; while ((ch =...
Kev K's user avatar
  • 17
0 votes
3 answers
128 views

Reallocating 1D array of integers, keeping original values, zeroing out rest

I would like to resize a one-dimensional array of integers, keep the values from the original array and initialise the new values with zeroes. So far, I have come up with two alternatives (a) using ...
mabalenk's user avatar
  • 981
1 vote
2 answers
138 views

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 <stdio.h> #include <stdlib.h> int ...
SreeT's user avatar
  • 21
2 votes
1 answer
61 views

Managing dynamically allocatable arrays inside a custom structure

I would like to define a custom structure that contains two dynamically allocatable integer arrays a and b. In order to allocate memory for the arrays and initialise arrays with values I have written ...
mabalenk's user avatar
  • 981
0 votes
1 answer
342 views

Initializing a char *tab[] with malloc/calloc

I’m trying to initialize a char *tab[] and this causes segmentation fault, what am I doing wrong? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <float.h> ...
Loukoum Mira's user avatar
0 votes
0 answers
277 views

Porting 32-bit to 64-bit C program "Exception Thrown: Read Access Violation 0xC0000005"

I am using VS2017 and have the following lines of C code (sorry can't post all contents of function): char *dptr; /* Global variables */ static u_int16_t vers; ... static ...
user10460902's user avatar
1 vote
2 answers
680 views

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 ...
S.NewUser's user avatar
1 vote
3 answers
682 views

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, &...
David Montgomery's user avatar
1 vote
2 answers
2k views

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 ...
TinSlam's user avatar
  • 11
4 votes
3 answers
2k views

Does calloc zero out the entire allocation?

The calloc function in C is used to allocate zeroed memory capable of holding at least the requested amount of elements of a specified size. In practice, most memory allocators may allocate a bigger ...
Aiueiia's user avatar
  • 161
2 votes
1 answer
2k views

Relation between calloc char array and null terminating character

This whole "debate" on the web about strncpy being safe vs unsafe is driving me crazy. I find some people saying strncpy is the "devil", which to me sounds like they lack the programming discipline. I ...
ThatsRightJack's user avatar
-1 votes
1 answer
112 views

Does my code leak memory?

I have a piece of code that takes a command line argument consisting of two numbers separated by a colon (e.g. 1:8) and converts it to the corresponding integer values: const char delimiter[] = ":"; ...
ci7i2en4's user avatar
  • 834
0 votes
1 answer
47 views

Allocate array in main inside a function which is defined outside of main

I'm trying to use calloc on array inside a function but it doesn't work. When trying to debug I found that inside the function the pointer points to the allocated memory but when leaving the function ...
user183748292's user avatar
0 votes
1 answer
281 views

C programming reading from file using functions & calloc

I am having trouble utilizing functions to get the second calloc of my program to read from file work correctly. The function to be called is colAlloc (variables). My code below is: void ...
Hadrian Hu's user avatar

1 2 3
4
5
12