Has anyone ever used the calloc and realloc commands in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Has anyone ever used the calloc and realloc commands in C

I'm having a hard time incorporating these two into a stack program I'm writing. Any suggestions welcomed.

6th Nov 2017, 9:26 PM
Kenneth Morales
Kenneth Morales - avatar
5 Answers
+ 4
use the realloc when you add an element and the stack is full but I do not see the use of calloc instead of malloc, is there a particular reason ?
6th Nov 2017, 11:00 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
Yes @Ace, I know this use but I do not know why a stack should be zero filled as a block is considered empty if the user do not add its own element in it
7th Nov 2017, 5:36 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Yes I have ! What is your problem ? :) edit : you should tag this post in C not C#
6th Nov 2017, 10:37 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
It will depend on your implementation of the of stack. If (in the background) it is a dynamically allocated array you can resize it using realloc. If it works like a linked list, then you can add new nodes (data blocks) to the list using calloc (or malloc). I cannot give any more detailed without knowing your data structures and functions.
7th Nov 2017, 8:15 AM
Jared Bird
Jared Bird - avatar
0
the program itself I have running it's a simple stack. It ask the user is they would like to push, pop or display what's in the stack which all work fine. The program also tells the users when the stack if full and empty. But I want to use realloc and calloc to make the stack bigger if full on the go but don't know how to incorporate them into the code.
6th Nov 2017, 10:44 PM
Kenneth Morales
Kenneth Morales - avatar