# Making an array dynamic

• 02-10-2009
Making an array dynamic
Hey there everyone, I'm doing a project for school and I have it completely working right now. There's 1 part of the project that I don't know how to do...Make the array that's inside of a struct, dynamic.

If anybody could help me, I'd greatly appreciate it.

Code:

```typedef struct {         VectorItemT items[DEFAULT_CAPACITY];         int size;         int capacity; }VectorT;```
Code:

```VectorT *newVector() {         VectorT *ptr_malloc = (VectorT*)malloc(sizeof(VectorT));         ptr_malloc->size = 0;         ptr_malloc->capacity = 5;                 return ptr_malloc; }```
These are in separate files.... Anybody got a clue where to start?
I've read several tutorials & other posts, it just doesn't click when I read them.
What else do you need to know?

• 02-10-2009
vart
you do not need to cast malloc in C

2.
Code:

```typedef struct {         VectorItemT* items;         size_t allocated_capacity;         int size;         int capacity; }VectorT; VectorT *newVector() {         VectorT *ptr_malloc = malloc(sizeof(VectorT));         if(!ptr_malloc) return NULL;         ptr_alloc->items = malloc(DEFAULT_CAPACITY * sizeof *ptr_alloc->items );         if(!ptr_malloc->items)                 ptr_malloc-> allocated_capacity = 0;         else                 ptr_malloc-> allocated_capacity = DEFAULT_CAPACITY;         ptr_malloc->size = 0;         ptr_malloc->capacity = 5;         return ptr_malloc; }```
• 02-10-2009
Thanks so much!

Would you mind going over this step-by-step so I know what's going on?

and did you just miss type ptr_malloc a couple times?

Code:

```VectorT *newVector() {         VectorT *ptr_malloc = malloc(sizeof(VectorT));         if(!ptr_malloc) return NULL; /////  \/ ptr_malloc?                                        \/ptr_malloc?         ptr_alloc->items = malloc(DEFAULT_CAPACITY * sizeof *ptr_alloc->items );         if(!ptr_malloc->items)                 ptr_malloc-> allocated_capacity = 0;         else                 ptr_malloc-> allocated_capacity = DEFAULT_CAPACITY;         ptr_malloc->size = 0;         ptr_malloc->capacity = 5;         return ptr_malloc; }```
• 02-10-2009
vart
Quote:

Thanks so much!

Would you mind going over this step-by-step so I know what's going on?

and did you just miss type ptr_malloc a couple times?

m key is too lazy on my keyboard

Code:

```VectorT *newVector() {         VectorT *ptr_malloc = malloc(sizeof(VectorT));         if(!ptr_malloc) return NULL; /* you need to check the return value of malloc */         ptr_malloc->items = malloc(DEFAULT_CAPACITY * sizeof *ptr_malloc->items ); /* allocate DEFAULT_CAPACITY items                 * size of each is sizeof *ptr_malloc->items -                 * if you change the type of the variable you do not need to change the sizeof operator as in your malloc */         if(!ptr_malloc->items)                 ptr_malloc-> allocated_capacity = 0; /* failed to allocate array */         else                 ptr_malloc-> allocated_capacity = DEFAULT_CAPACITY; /* store the array size to prevent buffer overrun,                 * if we will need more than this -                 * use realloc to increase size and update this var accordingly */         ptr_malloc->size = 0;         ptr_malloc->capacity = 5;         return ptr_malloc; }```
• 02-10-2009
matsp
Probably shouldn't do:
Code:

`        ptr_malloc->capacity = 5;`
if the malloc() failed!

--
Mats
• 02-10-2009
bel5f5
Variable-length array
Code:

```typedef struct {         int size;         int capacity;         VectorItemT items[0]; /*This must be the last member!                                               And if your compiler complains,                                               try changing it to "VectorItemT items[];"*/ }VectorT;```
Code:

```VectorT *newVector(int capacity) {         VectorT *ptr_malloc = (VectorT*)malloc(sizeof(VectorT) + capacity * sizeof(VectorItemT));         if (NULL != ptr_malloc) {             ptr_malloc->size = 0;             ptr_malloc->capacity =  capacity;         }         return ptr_malloc; }```
• 02-10-2009