memory allocation in C

Status
Not open for further replies.

eugensth

Solid State Member
Messages
8
Hi,

I have a problem with memory allocation in C. After allocating with malloc a matrix of integers :

int*a = malloc(10 * sizeof (int));

I would like to free the memory occupated by some values like a[3], or a[5], but keeping the other elements of the matrix and the matrix itself.
Can anybody help me?

Thanks anticipated
 
The only way to do this that I would reccomend, would be creating a dynamically resizable array.

Start with 10, if you like. As you delete one, create a new array of 9. Copy all remaining elements to that one, and free() the original array of 10.
 
But what happens when I deal with big matrixes of structures ? I guess this will consume a lot of resources, ain't?
 
This is how its always been done if you want to use simple arrays and matrices.

Unless you would like to use a linked list instead....
 
Status
Not open for further replies.
Back
Top Bottom