Here I have a pointer to the first element and int to hold the number of elements. How do I add in malloc and calloc for memory allocation?
struct vector_new
{
char *start;
int count;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are looking for a “dynamic array” implementation.
You keep track of both how many objects are currently in the array and how much space is allocated for it. When you need more space you call
reallocand ask forcurrent_size * factorwherefactoris greater than one. Typical values forfactorare between 1.4 and 2.It can be shown that the amortized cost of appending
nitems to the array is O(n).Note that this is not efficient if you want to insert stuff into the middle. That’s a different critter.