The term array both in C and in C++ is used to denote a contiguous block of memory containing one or more data elements of the same type.
There are two types of arrays: static arrays and dynamic arrays. static arrays are allocated in the memory when the program is compiled and stored in the data segment of your program. Dynamic arrays as their name implies are allocated in runtime from the heap (the data structure used to access the memory available to the program for dynamic allocation).
Both array types are accessed the same way using pointers and indexers (the [] operator).
Both in C and C++ a pointer is a variable that point to a place in memory. The memory it points to may contain a class or a primitive data type.
There are however differences between a static arrays and a dynamic arrays. Here are the important ones:
- dynamic arrays must be allocated to use them and freed to prevent memory leaks
- static arrays cannot be resized
In C you allocate memory using malloc
and release it using free
. You can resize it using realloc
In C++ you allocate memory using new
and release it using delete
. You have to write your own code to resize it.
For most purposes a dynamic array in C++ will be implemented using std::vector
. In C if you want such a mechanism you will have to roll your own. You got a good answer from @Cheers and hth. - Alf on std::vector
.
I don't think this is the place to write a full tutorial on dynamic memory in C or C++. You can get a good introduction from http://www.cplusplus.com/doc/tutorial/dynamic/
Here is however a sample code that will show you how to do what you want in C. For C++ use std::vector
.
/*
C CODE
*/
#include <stdlib.h>
#include <limits.h>
typedef struct DARRAY dynamic_int_array_t;
typedef struct DARRAY
{
int length;
int *data;
void (*alloc)(dynamic_int_array_t * the_array, int length);/*allocate*/
void (*free)(dynamic_int_array_t * the_array);/*free*/
void (*resize)(dynamic_int_array_t * the_array, int new_length);/*resize*/
void (*append)(dynamic_int_array_t * the_array, int new_value);
int (*get_at)(dynamic_int_array_t * the_array, int pos);/*use 1 based index getter*/
void (*set_at)(dynamic_int_array_t * the_array, int pos, int value);/*use 1 based index setter*/
}dynamic_int_array_t;
void allocIntArray(dynamic_int_array_t * the_array, int length)
{
/*
Allocate memory to contain length number of integers
*/
the_array->data = (int*)malloc(length*sizeof(int));
the_array->length = length;
}
void freeIntArray(dynamic_int_array_t * the_array)
{
if(NULL != the_array->data)
free(the_array->data);/*Free the memory*/
the_array->length = 0;/*Reset the length of the array*/
}
void resizeIntArray(dynamic_int_array_t * the_array, int new_length)
{
if(NULL != the_array->data && the_array->length > 0 && new_length >= 0)
{
if(new_length == 0)/*Free the array if a value of 0 was requested*/
the_array->free(the_array);
else
{
/*Resize the memory block*/
the_array->data = (int*)realloc(the_array->data, new_length*sizeof(int));
}
the_array->length = new_length;
}
}
void appendIntArray(dynamic_int_array_t * the_array, int new_value)
{
the_array->resize(the_array, the_array->length + 1);
the_array->data[the_array->length - 1] = new_value;
}
int getAt(dynamic_int_array_t * the_array, int pos)
{
if(NULL != the_array->data && pos > 0 && pos <= the_array->length)
return the_array->data[pos-1];
return INT_MIN;/*use INT_MIN to indicate error*/
}
void setAt(dynamic_int_array_t * the_array, int pos, int value)
{
if(NULL != the_array->data && pos > 0 && pos <= the_array->length)
the_array->data[pos-1] = value;
}
int main(int argc, char **argv)
{
dynamic_int_array_t int_array =
{
0,
NULL,
allocIntArray,
freeIntArray,
resizeIntArray,
appendIntArray,
getAt,
setAt
};
int_array.alloc(&int_array, 4);
int_array.data[0] = 1, int_array.data[1] = 2,
int_array.data[2] = 3, int_array.data[3] = 4;
printf("array length: %d, last member: %d\n", int_array.length, int_array.get_at(&int_array,4));
int_array.resize(&int_array, 5);
int_array.data[4] = 5;/* can also use int_array.set_at(&int_array,5) = 5; */
printf("array length: %d, last member: %d\n", int_array.length, int_array.get_at(&int_array,5));
int_array.append(&int_array, 6);
printf("array length: %d, last member: %d\n", int_array.length, int_array.get_at(&int_array,5));
}