Is there a way to initialize an array without defining the size. The size of an array increases on its own as as when the loop runs it reallocates the array.
-
Use C++, it has std::vector. – rubenvb Apr 28 '13 at 06:48
-
6@rubenvb: So, your answer to "how do I accomplish X in C" is "use C++"? Really? You are aware that many projects are written in C, right? – Ed S. Apr 28 '13 at 06:50
-
OP, you would have to create your own data structure or use an existing, third party alternative. C does not provide anything like this out of the box. – Ed S. Apr 28 '13 at 06:51
-
@EdS. It's not an answer, but a comment. It seems the OP is still learning and might not know about this. – rubenvb Apr 28 '13 at 06:53
-
2@rubenvb: Yes, the OP is learning C, not C++. Different languages. – Ed S. Apr 28 '13 at 06:54
-
If one had perfect control of the runtime, then it would be possible to insert a signal handler to trap stack overflow, increase stack from system memory, insert entries to VTLB and use a custom initializer callback function. Doesn't sound beginner stuff though. – Aki Suihkonen Apr 28 '13 at 06:55
-
@AkiSuihkonen: Sounds like a terrible idea either way since dynamic allocation exists. – Ed S. Apr 28 '13 at 06:56
-
See this question, which has a great answer: http://stackoverflow.com/questions/3536153/c-dynamically-growing-array – Anil Vaitla Apr 28 '13 at 07:22
-
@EdS. The OP wants to have dynamic allocation without the syntax traditionally required for it. – Aki Suihkonen Apr 28 '13 at 08:56
3 Answers
There is no such thing out of the box. You will have to create your own array-like data structure that does this. It shouldn't be very hard to implement, if you're careful.
What you're looking for is, roughly, a data structure that, when created, allocates (using malloc
, for instance) a predefined size and starts using the consecutive space inside it as slots of an array. Then, as more items are added, it reallocates (say, using realloc
) that space.
Of course, you won't be able to use the indexer syntax you're used to with simple arrays. Instead, your data structure will have to provide its own pair of set
/get
functions that take care of the above, under the hood. Therefore, the set
function will check the index specified in its arguments and, if that index is greater than the current size of the array, perform a reallocation. Then, in any case, set the value provided to the specified index.

- 28,773
- 8
- 68
- 104
You can initialize an array without specifying the size but it would not be useful unless you allocated space for it before you used it. Normally when you declare a variable in C, the compiler reserves a specific amount of memory for that variable on the "stack". If you want an array to be able to grow throughout the program, however, this is not what you are looking for because the amount of space allocated for a variable on the "stack" is static.
The solution, therefore, is to have the program decide how much memory to allocate to your variable at run-time, instead of compile-time. This way, while the program is running, you will be able to decide how much space your variable needs to have reserved.
In practice, this is called dynamic memory allocation and it is accomplished in C using the functions malloc() and realloc(). I would suggest reading up on these functions, I think they will be very useful to you.
If you have follow up questions feel free to ask.
One last thing!
Whenever you use malloc() to allocate memory for a variable, you should remember to call the function free() on that variable at the end of the program or whenever you are done using the variable.

- 1,027
- 3
- 13
- 22
-
2A program has memory leaks when it allocates memory that it cannot access later (because it has lost the address, for example). A program has memory leaks at runtime. If you release your dynamically allocated memory at the end of the program (instead of releasing it as soon as that memory isn't useful anymore), you're not solving any leaks. The only thing that makes sense to run at the end of the program is any finalizer patterns you may have in your code. Other than that, the OS reclaims all the memory of your process, regardless of whether your program could access it (in a valid way). – Theodoros Chatzigiannakis Apr 28 '13 at 07:52
-
Thanks for the clarification! My knowledge of C comes entirely from one class I have taken so I appreciate the correction. I have edited my post, is it accurate now? – Dsel Apr 28 '13 at 07:55
Here's a simple implementation of such a datastructure (for int
s, but you can replace the int
with whatever type you need). I've omitted error-handling for clarity.
typedef struct array_s {
int len, cap;
int *a;
} array_s, *array_t;
/* Create a new array with 0 length, and the given capacity. */
array_t array_new(int cap) {
array_t result = malloc(sizeof(array_s));
array_s a = {0, cap, malloc(sizeof(int) * cap)};
*result = a;
return result;
}
/* Destroy an array. */
void array_free(array_t a) {
free(a->a);
free(a);
}
/* Change the size of an array, truncating if necessary. */
void array_resize(array_t a, int new_cap) {
result->cap = new_cap;
result->a = realloc(result->a, new_cap * sizeof(int));
if (result->len > result->cap) {
result->len = result->cap;
}
}
/* Add a new element to the end of the array, resizing if necessary. */
void array_append(array_t a, int x) {
if (result->len == result->cap) {
// max the new size with 4 in case cap is 0.
array_resize(a, max(4, result->cap * 2));
}
a->a[a->len++] = x;
}
By storing len
(the current length of the array), and cap
(the amount of space you've reserved for the array), you can extend the array in O(1) up to the point when len
is cap
, then resize the array (eg: using realloc
), perhaps by multiplying the existing cap
by 2 or 1.5 or something. This is what most vector or list types do in languages that support resizable arrays. I've coded this in array_append
as an example.

- 54,811
- 11
- 92
- 118
-
See also http://stackoverflow.com/questions/3536153/c-dynamically-growing-array (which I hadn't seen when I wrote my answer). It's essentially the same idea and very similar code. – Paul Hankin Apr 28 '13 at 08:08
-
You refer to `result` in a few APIs, but it is locally defined in `array_new()`, or do I miss something? – ysap Nov 01 '17 at 14:02