Dynamically allocate an array of unspecified size in C

Comment hidden because of low score. Click to expand.
of 0 vote

Use techniques used to implement std::vector.

- Anonymous May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Array basically does not have a bound check. So, U can extend till the memory allows you but that ain't a good code practice.
Conceptually, U can use vector allocation method.

- hprem991 May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
of 2 vote

You would probably want to use the same kind of techniques that std::vector uses. A simple implementation would be to make an array of some initial size, and whenever you run out of space, make a new array that's twice as large, copy the contents over to it, and use that as your new array.

In C, for a growable array of type T (might be char in your case -- I didn't make the code use templates because pure C doesn't have them) this might look something like:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ArrayList 
  int capacity;
  int size;
  T* arr;
//initialCapacity must be a positive integer -- cannot be 0 in this implementation.
struct ArrayList* ArrayList_create(int initialCapacity)
  struct ArrayList* newList = (struct ArrayList*) malloc(sizeof(struct ArrayList));
  newList->capacity = initialCapacity;
  newList->size = 0;
  newList->arr =  (T*) malloc(sizeof(T)*initialCapacity);
  return newList;
void ArrayList_add (struct ArrayList* list, T elem)
  if (list->size == list->capacity)
    //double the capacity
    list->capacity *= 2;
    //make a new array with the new capacity
    T* newArr = (T*) malloc(sizeof(T)*list->capacity);
    //move the data over
    memcpy (newArr, list->arr, sizeof(T)*list->size);
    list->arr = newArr;    
  //now that sufficient capacity is ensured, add the new element
  list->arr[list->size] = elem;

void ArrayList_print (struct ArrayList* list)
  if (list->size == 0) { return; }
  for (int i=0; i < list->size - 1; i++)
    //printElement is a placeholder for printing an element of type T
    printElement (list->arr[i]);
    printf (", ");
  printElement (list->arr[list->size - 1]);
//main method to test above code
int main()
  struct ArrayList* list =ArrayList_create(1);
  ArrayList_add(list, 2);
  ArrayList_add(list, 5);
  ArrayList_add(list, 7);
  ArrayList_add(list, 1);
  ArrayList_add(list, 11);
  return 0;

I tested a working version of this code for integers here: ideone .com/pOdIr2

- eugene.yarovoi May 23, 2013 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More